What is Toggle Button

Android Toggle Button is allow the user to change the state between on and off.

In my prgram when the program run the TextView is invisible and ToggleButton Text is OFF when the user click on ToggleButton then the TextView is visible and the ToggleButton Text is ON.When user again click on ToggleButton it invisible the TextView and ToggleButton Text is OFF.


ToggleButton:.

In this section i am going to show the ToggleButton with practical example.

First open the android studio and create a project and give the name of your project like ToggleButtonDemo.

The screenshots of the program is as follows:

Smiley face Smiley face

Drag and drop the one TextView and one ToggleButton. Open the activity_main.xml file and code look like this


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.niitpc.togglebutton.MainActivity">


    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginStart="111dp"
        android:layout_marginTop="98dp"
        android:visibility="invisible"
        android:text="TextView is visible" />

    <ToggleButton
        android:id="@+id/toggleButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignStart="@+id/textView"
        android:layout_below="@+id/textView"
        android:layout_marginTop="38dp"
        android:text="ToggleButton" />
</RelativeLayout>


       

Open the MainActivity.java file and write code look like this.


package com.example.niitpc.togglebutton;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.CompoundButton;
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);

    toggleButton.setOnCheckedChangeListener(
      new CompoundButton.OnCheckedChangeListener() {
      @Override
      public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
      if(isChecked)
      {
           textView.setVisibility(View.VISIBLE);
      }
      else
      {
           textView.setVisibility(View.INVISIBLE);
      }
     }
    }

  );

 }
}



Explanation:

ToggleButton toggleButton;:This statement create the object toggleButton of ToggleButton.

TextView textView:This statement create the object textView of TextView.

toggleButton=(ToggleButton)findViewById(R.id.toggleButton):Here we have to initialized the toggleButton object.

textView=(TextView)findViewById(R.id.textView)This statement initialized the textView object.

toggleButton.setOnCheckedChangeListener():This statement create the listener for toggle button.

public void onCheckedChanged(CompoundButton buttonView, boolean isChecked):This method active when user click on toggle button it has two argument the second return the state in boolean true or false.

if(isChecked):This statement check the condition true.If condtion is true then it will execute the code with in body.

textView.setVisibility(View.VISIBLE):This statement visible the textView.

elsethis statement execute when the if condition is false.With in body it invisible the textView


ToggleButton

what is ToggleButton for video click here

Smiley face                     Smiley face