Monday, 30 June 2014

(20) Android Toggle Button Example

Android Toggle Button Example

Toggle Button can be used to display checked/unchecked (On/Off) state on the button. It can be used to On/Off Sound, Wifi, Bluetooth etc.

ToggleButton class

ToggleButton class provides the facility of creating the toggle button.

Constants of ToggleButton class

  • 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

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

Example of ToggleButton

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. }  


Output:

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

No comments:

Post a Comment

any queries pls tel me