Android ToggleButton Example

Android Toggle Button Example Code

          Android Toggle Button can be utilized to show checked/unchecked (On/Off) state on the catch.

It is helpful if client need to change the setting between two states. It can be utilized to On/Off Sound, Wifi, Bluetooth and so on.

Since Android 4.0, there is another kind of flip catch called switch that gives slider control.

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

Android ToggleButton class

ToggleButton class gives the office of making the switch catch.

XML Attributes of ToggleButton class

The 3 XML traits of ToggleButton class.


XML AttributeDescription
android:disabledAlphaThe alpha to apply to the indicator when disabled.
android:textOffThe text for the button when it is not checked.
android:textOnThe text for the button when it is checked.

Methods of ToggleButton class


The broadly utilized strategies for ToggleButton class are given underneath

MethodDescription
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 switch catch and one catch for the design. Presently the activity_main.xml document will resemble this:

  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

How about we compose the code to check which flip catch is ON/OFF.
  1. package com.dheeruapps.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. }  

Output:

0 comments:

Post a Comment

My Instagram