fastread homefastrread library fastread menu

Android : ToggleButton Example

Tutorial by:      Date: 2016-05-10 05:00:10

❰ Previous Next ❱

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.   
  7.     <ToggleButton  
  8.         android:id="@+id/toggleButton1"  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"  
  11.         android:layout_alignParentLeft="true"  
  12.         android:layout_alignParentTop="true"  
  13.         android:layout_marginLeft="60dp"  
  14.         android:layout_marginTop="18dp"  
  15.         android:text="ToggleButton1"  
  16.         android:textOff="Off"  
  17.         android:textOn="On" />  
  18.   
  19.     <ToggleButton  
  20.         android:id="@+id/toggleButton2"  
  21.         android:layout_width="wrap_content"  
  22.         android:layout_height="wrap_content"  
  23.         android:layout_alignBaseline="@+id/toggleButton1"  
  24.         android:layout_alignBottom="@+id/toggleButton1"  
  25.         android:layout_marginLeft="44dp"  
  26.         android:layout_toRightOf="@+id/toggleButton1"  
  27.         android:text="ToggleButton2"  
  28.         android:textOff="Off"  
  29.         android:textOn="On" />  
  30.   
  31.     <Button  
  32.         android:id="@+id/button1"  
  33.         android:layout_width="wrap_content"  
  34.         android:layout_height="wrap_content"  
  35.         android:layout_below="@+id/toggleButton2"  
  36.         android:layout_marginTop="82dp"  
  37.         android:layout_toRightOf="@+id/toggleButton1"  
  38.         android:text="submit" />  
  39.   
  40. </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.   
  3. import android.os.Bundle;  
  4. import android.app.Activity;  
  5. import android.view.Menu;  
  6. import android.view.View;  
  7. import android.view.View.OnClickListener;  
  8. import android.widget.Button;  
  9. import android.widget.Toast;  
  10. import android.widget.ToggleButton;  
  11.   
  12. public class MainActivity extends Activity {  
  13.     private ToggleButton toggleButton1, toggleButton2;  
  14.     private Button buttonSubmit;  
  15.     @Override  
  16.     protected void onCreate(Bundle savedInstanceState) {  
  17.         super.onCreate(savedInstanceState);  
  18.         setContentView(R.layout.activity_main);  
  19.           
  20.         addListenerOnButtonClick();  
  21.     }  
  22.     public void addListenerOnButtonClick(){  
  23.         //Getting the ToggleButton and Button instance from the layout xml file  
  24.         toggleButton1=(ToggleButton)findViewById(R.id.toggleButton1);  
  25.         toggleButton2=(ToggleButton)findViewById(R.id.toggleButton2);  
  26.         buttonSubmit=(Button)findViewById(R.id.button1);  
  27.   
  28.         //Performing action on button click  
  29.         buttonSubmit.setOnClickListener(new OnClickListener(){  
  30.   
  31.             @Override  
  32.             public void onClick(View view) {  
  33.                 StringBuilder result = new StringBuilder();  
  34.                    result.append("ToggleButton1 : ").append(toggleButton1.getText());  
  35.                    result.append("\nToggleButton2 : ").append(toggleButton2.getText());  
  36.                 //Displaying the message in toast  
  37.                 Toast.makeText(getApplicationContext(), result.toString(),Toast.LENGTH_LONG).show();  
  38.             }  
  39.               
  40.         });  
  41.           
  42.     }  
  43.     @Override  
  44.     public boolean onCreateOptionsMenu(Menu menu) {  
  45.         // Inflate the menu; this adds items to the action bar if it is present.  
  46.         getMenuInflater().inflate(R.menu.activity_main, menu);  
  47.         return true;  
  48.     }  
  49.   
  50. }  

❰ Previous Next ❱