C1CTech

Android ToggleButton Example

Toggle Buttons

A toggle button allows the user to change a setting between two states.

You can add a basic toggle button to your layout with the ToggleButton object. Android 4.0 (API level 14) introduces another kind of toggle button called a switch that provides a slider control, which you can add with a Switch object.

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

Get GITHUB code from HERE.

 

Creating New Project

  1. In Android Studio, go to File ⇒ New Project and fill all the details required to create a new project. When it prompts to select a default activity, select Blank Activity and proceed.
  2. In activity_main.xml  I have add  one ToggleButton with following basic properties.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<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"
    tools:context="com.example.lenovo.togglebuttonapp.MainActivity">


    <ToggleButton
        android:id="@+id/toggle_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:background="@android:color/holo_red_light"
        android:padding="30dp"
        android:textOff="DISABLE"
        android:textOn="ENABLE"
        android:textSize="25sp"
        android:textStyle="bold" />

</RelativeLayout>

XML Attributes of ToggleButton class used:

android:textOff  :  The text for the button when it is off.

android:textOn  :   The text for the button when it is on.

3.Now open  MainActivity.java and write the below code.

To detect when the user activates the button , create an CompoundButton.OnCheckedChangeListener object and assign it to the button by calling setOnCheckedChangeListener().

MainActivity.java

package com.example.lenovo.togglebuttonapp;

import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.CompoundButton;
import android.widget.Toast;
import android.widget.ToggleButton;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final ToggleButton toggle = (ToggleButton) findViewById(R.id.toggle_btn);

        toggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                if (isChecked) {
                    // The toggle is enabled
                    toggle.setBackgroundColor(Color.GREEN);
                    Toast.makeText(getApplicationContext(), "ToggleButton is ON   ", Toast.LENGTH_LONG).show();
                } else {
                    // The toggle is disabled
                    toggle.setBackgroundColor(Color.RED);
                    Toast.makeText(getApplicationContext(), "ToggleButton is OFF   ", Toast.LENGTH_LONG).show();
                }
            }
        });
    }
}


Now when you run the app it will look like this:

Screenshot_1580911231   

 

 

Exit mobile version