How to add Toggle Button in an Android Application

ToggleButton is basically a stop / play or on/off button with indicator light indicating the current state of ToggleButton. ToggleButton is widely used, some examples are on/off audio, Bluetooth, WiFi, hot-spot etc. This is a subclass of Composite Button.

ToggleButton allows users to change settings between two states from your phone’s Settings menu such as turning your WiFi, Bluetooth, etc. on / off. Since the Android 4.0 version (API level 14), it has another type of toggle button called switch which provides user slider control.

Programmatically, isChecked() method is used to check the current state of the toggle button. This method returns a boolean value. If a toggle button is ON, this returns true otherwise it returns false. Below is the example in which toggle button is used.

Approach

  • Step 1: Create a new project and fill all the required details for the app like the app name, package name, etc.



    Select File -> New -> New Project -> fill required details and click “Finish”

  • Step 2: In this step, open the XML file and add the code to display the toggle button and a textview.

    res -> Layout -> Activity_Main.xml (or) Main.xml

    acticity_main.xml

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"  
        xmlns:tools="http://schemas.android.com/tools"  
        android:layout_width="match_parent"  
        android:layout_height="match_parent"  
        android:padding="16dp"  
        android:background="@color/white"  
        tools:context=".MainActivity">  
      
        <ToggleButton  
            android:id="@+id/toggleButton"  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:layout_centerHorizontal="true"  
            android:layout_centerVertical="true"  
            android:onClick="onToggleClick"  
            />  
        <TextView  
            android:id="@+id/textView"  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:layout_marginBottom="100dp"  
            android:layout_centerVertical="true"  
            android:layout_centerHorizontal="true"  
            android:layout_above="@+id/toggleButton"
            android:textStyle="bold"  
            android:textColor="@color/black"/>  
    </RelativeLayout>  

    chevron_right

    
    

  • Step 3: In this step, open MainActivity and add the below code to initialize the toggle button and add onToggleClick method which will be invoked when the user clicks on the toggle button. This method changes the text in textview.

    Open the app -> Java -> Package -> Mainactivity.java

    MainActivity.Java

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    import androidx.appcompat.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.TextView;
    import android.widget.ToggleButton;
      
    public class MainActivity
        extends AppCompatActivity {
      
        ToggleButton togglebutton;
        TextView textview;
        @Override
        protected void onCreate(
            Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
      
            togglebutton
                = (ToggleButton)findViewById(
                    R.id.toggleButton);
      
            textview
                = (TextView)findViewById(
                    R.id.textView);
        }
      
        public void onToggleClick(View view)
        {
            if (togglebutton.isChecked()) {
                textview.setText("Toggle is ON");
            }
            else {
                textview.setText("Toggle is OFF");
            }
        }
    }

    chevron_right

    
    

Output:

Now connect your device with USB cable and launch the application. You will see a toggle button. Click on the toggle button which will display the status of the toggle button.

Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.