Android ToggleButton Example

  • 0

Android ToggleButton Example

Category : Uncategorized

android toggle button

Android Toggle Button can be used to display checked/unchecked (On/Off) state on the button.

It is beneficial if user have to change the setting between two states. It can be used to On/Off Sound, Wifi, Bluetooth etc.

Since Android 4.0, there is another type of toggle button called switch that provides slider control.

Android ToggleButton and Switch both are the subclasses of CompoundButton class.

Android ToggleButton class

ToggleButton class provides the facility of creating the toggle button.

XML Attributes of ToggleButton class

The 3 XML attributes of ToggleButton class.

XML Attribute Description
android:disabledAlpha The alpha to apply to the indicator when disabled.
android:textOff The text for the button when it is not checked.
android:textOn The text for the button when it is checked.

Methods of ToggleButton class

The widely used methods of ToggleButton class are given below.

Method Description
CharSequence getTextOff() Returns the text when button is not in the checked state.
CharSequence getTextOn() Returns the text for when button is in the checked state.
void setChecked(boolean checked) Changes the checked state of this button.

Android ToggleButton Example

activity_main.xml

Drag two toggle button and one button for the layout. Now the activity_main.xml file will look like this:

File: activity_main.xml
  1. <RelativeLayout xmlns:androclass=“http://schemas.android.com/apk/res/android”
  2.     xmlns:tools=“http://schemas.android.com/tools”
  3.     android:layout_width=“match_parent”
  4.     android:layout_height=“match_parent”
  5.     tools:context=“.MainActivity” >
  6.     <ToggleButton
  7.         android:id=“@+id/toggleButton1”
  8.         android:layout_width=“wrap_content”
  9.         android:layout_height=“wrap_content”
  10.         android:layout_alignParentLeft=“true”
  11.         android:layout_alignParentTop=“true”
  12.         android:layout_marginLeft=“60dp”
  13.         android:layout_marginTop=“18dp”
  14.         android:text=“ToggleButton1”
  15.         android:textOff=“Off”
  16.         android:textOn=“On” />
  17.     <ToggleButton
  18.         android:id=“@+id/toggleButton2”
  19.         android:layout_width=“wrap_content”
  20.         android:layout_height=“wrap_content”
  21.         android:layout_alignBaseline=“@+id/toggleButton1”
  22.         android:layout_alignBottom=“@+id/toggleButton1”
  23.         android:layout_marginLeft=“44dp”
  24.         android:layout_toRightOf=“@+id/toggleButton1”
  25.         android:text=“ToggleButton2”
  26.         android:textOff=“Off”
  27.         android:textOn=“On” />
  28.     <Button
  29.         android:id=“@+id/button1”
  30.         android:layout_width=“wrap_content”
  31.         android:layout_height=“wrap_content”
  32.         android:layout_below=“@+id/toggleButton2”
  33.         android:layout_marginTop=“82dp”
  34.         android:layout_toRightOf=“@+id/toggleButton1”
  35.         android:text=“submit” />
  36. </RelativeLayout>

Activity class

Let’s write the code to check which toggle button is ON/OFF.

File: MainActivity.java
  1. package com.example.togglebutton;
  2. import android.os.Bundle;
  3. import android.app.Activity;
  4. import android.view.Menu;
  5. import android.view.View;
  6. import android.view.View.OnClickListener;
  7. import android.widget.Button;
  8. import android.widget.Toast;
  9. import android.widget.ToggleButton;
  10. public class MainActivity extends Activity {
  11.     private ToggleButton toggleButton1, toggleButton2;
  12.     private Button buttonSubmit;
  13.     @Override
  14.     protected void onCreate(Bundle savedInstanceState) {
  15.         super.onCreate(savedInstanceState);
  16.         setContentView(R.layout.activity_main);
  17.         addListenerOnButtonClick();
  18.     }
  19.     public void addListenerOnButtonClick(){
  20.         //Getting the ToggleButton and Button instance from the layout xml file
  21.         toggleButton1=(ToggleButton)findViewById(R.id.toggleButton1);
  22.         toggleButton2=(ToggleButton)findViewById(R.id.toggleButton2);
  23.         buttonSubmit=(Button)findViewById(R.id.button1);
  24.         //Performing action on button click
  25.         buttonSubmit.setOnClickListener(new OnClickListener(){
  26.             @Override
  27.             public void onClick(View view) {
  28.                 StringBuilder result = new StringBuilder();
  29.                    result.append(“ToggleButton1 : “).append(toggleButton1.getText());
  30.                    result.append(“\nToggleButton2 : “).append(toggleButton2.getText());
  31.                 //Displaying the message in toast
  32.                 Toast.makeText(getApplicationContext(), result.toString(),Toast.LENGTH_LONG).show();
  33.             }
  34.         });
  35.     }
  36.     @Override
  37.     public boolean onCreateOptionsMenu(Menu menu) {
  38.         // Inflate the menu; this adds items to the action bar if it is present.
  39.         getMenuInflater().inflate(R.menu.activity_main, menu);
  40.         return true;
  41.     }
  42. }


Output:

android toggle button example output 1 android toggle button example output 2


Leave a Reply