Thursday, 9 March 2017

Android Toggle Button Example

The Android Toggle Button allows the user to switch between two states, ON and OFF. Ideal if you want your app to check or uncheck the on/off states of Sound, Bluetooth, WiFi, Location etc.

Android Toggle Button Example:

For this example all that's needed is 2 toggle buttons and a regular button in the layout.

File: content_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    android:id="@+id/content_main"
    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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="android.google.com.uiexample.MainActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="40dp"
        android:text="Toggle Button Example"
        android:textColor="?android:attr/editTextColor"
        android:textSize="24sp"
        android:textStyle="normal|bold"/>

    <ToggleButton
        android:id="@+id/toggleButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignStart="@+id/textView"
        android:layout_below="@+id/textView"
        android:layout_marginTop="50dp"/>

    <ToggleButton
        android:id="@+id/toggleButton2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignEnd="@+id/textView"
        android:layout_alignBottom="@+id/toggleButton1"
        android:checked="true"/>

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/toggleButton1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="125dp"
        android:text="PRESS TO CHECK"/>
</RelativeLayout>

Java Activity 

File: MainActivity.java
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.Toast;
import android.widget.ToggleButton;

public class MainActivity extends AppCompatActivity
{

  private ToggleButton toggleButton1, toggleButton2;
  private Button buttonCheck;


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

    getWidgets();
  }


  private void getWidgets()
  {
    //Getting the ToggleButtons and Button instance from the layout xml file
    toggleButton1 = (ToggleButton) findViewById(R.id.toggleButton1);
    toggleButton2 = (ToggleButton) findViewById(R.id.toggleButton2);
    buttonCheck = (Button) findViewById(R.id.button);

    //Performing action on toggle button 1 click
    toggleButton1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
    {
      public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
      {
        if (isChecked)
        {
          // The toggle is enabled
          Toast.makeText(MainActivity.this, "toggleButton1 is checked", Toast.LENGTH_SHORT).show();
        }
        else
        {
          // The toggle is disabled
          Toast.makeText(MainActivity.this, "toggleButton1 is disabled", Toast.LENGTH_SHORT).show();
        }
      }
    });

    //Performing action on toggle button 2 click
    toggleButton2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
    {
      public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
      {
        if (isChecked)
        {
          // The toggle is enabled
          Toast.makeText(MainActivity.this, "toggleButton2 is checked", Toast.LENGTH_SHORT).show();
        }
        else
        {
          // The toggle is disabled
          Toast.makeText(MainActivity.this, "toggleButton2 is disabled", Toast.LENGTH_SHORT).show();
        }
      }
    });


    //Performing action on button click
    buttonCheck.setOnClickListener(new View.OnClickListener()
    {
      @Override
      public void onClick(View view)
      {
        // retreives either ON or OFF text from togglebuttons
        String result = "ToggleButton1 : " + toggleButton1.getText() +
          "\nToggleButton2 : " + toggleButton2.getText();

        //Displaying the result string in a toast
        Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show();
      }
    });

  }

}

Result:



No comments:

Post a Comment