Android ToggleButton example

Android ToggleButton is UI widget which has only two states i.e. ON and OFF. It allows user to change setting between these two states.
Example :
You can use these button to change On and Off state of WiFi or Bluetooth

Source code:

Step 1 :Creating Project

Create an android application project named "ToggleButtonExampleApp".

Step 2 : Creating Layout

Change res ->layout -> activity_main.xml as below:
<?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:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:gravity="center_horizontal"
    tools:context="com.java2blog.togglebuttonexampleapp.MainActivity">

    <ToggleButton
        android:id="@+id/toggleButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="ToggleButton" >
    </ToggleButton>
</RelativeLayout>

Step 3 : Creating MainActivity

Change src/main/packageName/MainActivity.java as below:
package com.java2blog.togglebuttonexampleapp;

import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
import android.widget.ToggleButton;
import android.view.View.OnClickListener;

import static android.webkit.WebSettings.PluginState.ON;

public class MainActivity extends AppCompatActivity {

    ToggleButton toggleButton;
    private Activity activity;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        activity = this;

        toggleButton = (ToggleButton) findViewById(R.id.toggleButton1);
        toggleButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                if (toggleButton.isChecked()) {

                    Toast.makeText(activity, "Toggle button is in ON State", Toast.LENGTH_LONG).show();
                } else {

                    Toast.makeText(activity, "Toggle button is in OFF State", Toast.LENGTH_LONG).show();
                }
            }
        });
    }
}

We are getting toggle  reference from layout file and then using ToggleButton's setOnClickListener to register event for state change.
ToggleButton's isChecked() method is used to know current state of ToggleButton whether it is on state or off state.

Step 4 : Running the app 

When you run the app, you will get below screen:
When you click on toggle button, it will go to on state.
When you again click on toggle button, it will go to off state.
We are done with Android ToggleButton example. Please comment if you are facing any issue with the code.

Written by Arpit:

If you have read the post and liked it. Please connect with me on Facebook | Twitter | Google Plus

 

Java tutorial for beginners Copyright © 2012