Tuesday, November 19, 2024
Google search engine
HomeLanguagesJavaHow to add Toggle Button in an Android Application

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




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

    
    
  • 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




    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");
            }
        }
    }

    
    

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.

RELATED ARTICLES

Most Popular

Recent Comments