Sunday, 21 May 2017

Android ToggleButton

Android ToggleButton
Android Toggle Button can be used to display checked/unchecked (On/Off) state on the button.
It is beneficial if user have to change the setting between two states. It can be used to On/Off Sound, Wi-Fi, and Bluetooth etc.
Since Android 4.0, there is another type of toggle button called switch that provides slider control.
Android ToggleButton and Switch both are the subclasses of CompoundButton Class.

Android ToggleButton class

ToggleButton class provides the facility of creating the toggle button.

XML Attributes of ToggleButton class

XML Attribute
Description
Android:disabledAlpha
The alpha to apply to the indicator when disabled.
android:textOff
The text for the button when it is not checked.
Android:textOn
The text for the button when it is checked.

Methods of ToggleButton class

The widely used method of ToggleButton class are given below.

Method
Description
CharSequence getTextOff()
Returns the text when button is not in the checked state
CharSequence getTextOn()
Returns the text for when button is in the checked state.
Void setChecked(Boolean checked)
Changes the checked state of this button.

Android CheckBox Example

Activity_main.xml

File: activity_main.xml

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ToggleButton
        android:id="@+id/toggleButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="60dp"
        android:layout_marginStart="60dp"
        android:layout_marginTop="18dp"
        android:text="ToggleButton1"
        android:textOff="Off"
        android:textOn="On"/>

    <ToggleButton
        android:id="@+id/toggleButton2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/toggleButton1"
        android:layout_alignBottom="@+id/toggleButton1"
        android:layout_marginLeft="44dp"
        android:layout_marginStart="44dp"
        android:layout_toRightOf="@+id/toggleButton1"
        android:layout_toEndOf="@+id/toggleButton1"
        android:text="ToggleButton2"
        android:textOn="On"
        android:textOff="Off"/>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/toggleButton2"
        android:layout_marginTop="82dp"
        android:layout_toRightOf="@+id/toggleButton1"
        android:layout_toEndOf="@+id/toggleButton1"
        android:text="submit"/>

</RelativeLayout>

Activity Class

File: MainActivity.java

package com.mahesh.androidissimples.togglebuttondemo;

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

public class MainActivity extends AppCompatActivity {
    ToggleButton toggleButton1,toggleButton2;
    Button buttonSubmit;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        addListenerOnButtonClick();
    }

    private void addListenerOnButtonClick() {
        //Getting the ToggleButton and Button instance from the layout xml file
        toggleButton1= (ToggleButton) findViewById(R.id.toggleButton1);
        toggleButton2= (ToggleButton) findViewById(R.id.toggleButton2);
        buttonSubmit= (Button) findViewById(R.id.button1);
        buttonSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                StringBuilder result=new StringBuilder();
                result.append("ToggleButton1:").append(toggleButton1.getText());
                result.append("ToggleButton2:").append(toggleButton2.getText());

                //Display the messagein toast
                Toast.makeText(getApplicationContext(),result.toString(),Toast.LENGTH_SHORT).show();
            }
        });
    }
}


Output

No comments:

Post a Comment