Tuesday, March 16, 2021

Android ToggleButton

Android ToggleButton Example

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 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 widely used methods of ToggleButton class are given below.

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 toggle button and one button for the layout. Now the activity_main.xml file will look like this:

File: activity_main.xml
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:app="http://schemas.android.com/apk/res-auto"  
  4.     xmlns:tools="http://schemas.android.com/tools"  
  5.     android:layout_width="match_parent"  
  6.     android:layout_height="match_parent"  
  7.     tools:context="example.javatpoint.com.togglebutton.MainActivity">  
  8.   
  9.     <ToggleButton  
  10.         android:id="@+id/toggleButton"  
  11.         android:layout_width="wrap_content"  
  12.         android:layout_height="wrap_content"  
  13.         android:layout_marginLeft="8dp"  
  14.         android:layout_marginTop="80dp"  
  15.         android:text="ToggleButton"  
  16.         android:textOff="Off"  
  17.         android:textOn="On"  
  18.         app:layout_constraintEnd_toStartOf="@+id/toggleButton2"  
  19.         app:layout_constraintStart_toStartOf="parent"  
  20.         app:layout_constraintTop_toTopOf="parent" />  
  21.   
  22.     <ToggleButton  
  23.         android:id="@+id/toggleButton2"  
  24.         android:layout_width="wrap_content"  
  25.         android:layout_height="wrap_content"  
  26.         android:layout_marginRight="60dp"  
  27.         android:layout_marginTop="80dp"  
  28.         android:text="ToggleButton"  
  29.         android:textOff="Off"  
  30.         android:textOn="On"  
  31.         app:layout_constraintEnd_toEndOf="parent"  
  32.         app:layout_constraintTop_toTopOf="parent" />  
  33.   
  34.     <Button  
  35.         android:id="@+id/button"  
  36.         android:layout_width="wrap_content"  
  37.         android:layout_height="wrap_content"  
  38.         android:layout_marginBottom="144dp"  
  39.         android:layout_marginLeft="148dp"  
  40.         android:text="Submit"  
  41.         app:layout_constraintBottom_toBottomOf="parent"  
  42.         app:layout_constraintStart_toStartOf="parent" />  
  43. </android.support.constraint.ConstraintLayout>  

Activity class

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

File: MainActivity.java
  1. package example.javatpoint.com.togglebutton;  
  2.   
  3. import android.support.v7.app.AppCompatActivity;  
  4. import android.os.Bundle;  
  5. import android.view.View;  
  6. import android.widget.Button;  
  7. import android.widget.Toast;  
  8. import android.widget.ToggleButton;  
  9.   
  10. public class MainActivity extends AppCompatActivity {  
  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.   
  18.         addListenerOnButtonClick();  
  19.     }  
  20.   
  21.     public void addListenerOnButtonClick(){  
  22.         //Getting the ToggleButton and Button instance from the layout xml file  
  23.         toggleButton1=(ToggleButton)findViewById(R.id.toggleButton);  
  24.         toggleButton2=(ToggleButton)findViewById(R.id.toggleButton2);  
  25.         buttonSubmit=(Button)findViewById(R.id.button);  
  26.   
  27.         //Performing action on button click  
  28.         buttonSubmit.setOnClickListener(new View.OnClickListener(){  
  29.   
  30.             @Override  
  31.             public void onClick(View view) {  
  32.                 StringBuilder result = new StringBuilder();  
  33.                 result.append("ToggleButton1 : ").append(toggleButton1.getText());  
  34.                 result.append("\nToggleButton2 : ").append(toggleButton2.getText());  
  35.                 //Displaying the message in toast  
  36.                 Toast.makeText(getApplicationContext(), result.toString(),Toast.LENGTH_LONG).show();  
  37.             }  
  38.   
  39.         });  
  40.   
  41.     }  
  42. }  

Output:

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

No comments:

Post a Comment

Inapp update

  Inapp update https://desk.zoho.com/portal/vegabirdtech/en/kb/articles/how-to-use-burp-suite-with-android-mobile