Saturday, April 4, 2015

How to use ToggleButton in Android

1. Android ToggleButton Example

activity_main.xml

<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"
    android:padding="16dp"
    tools:context=".MainActivity"
    >
    <TextView
        android:id="@+id/tv"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="Click the ToggleButton"
        />
    <ToggleButton
        android:id="@+id/tb"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:onClick="onToggleButtonClicked"
        android:layout_below="@id/tv"
        />
</RelativeLayout>
MainActivity.java

package com.cfsuman.me.androidexamples;

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

//additional imported classes
import android.app.Activity;
import android.view.View;
import android.widget.ToggleButton;
import android.widget.TextView;

public class MainActivity extends Activity {

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

    public void onToggleButtonClicked(View v){
        //Get reference of the TextView from XML layout
        TextView tView = (TextView) findViewById(R.id.tv);

        //Check, is the toggle is on?
        boolean on = ((ToggleButton) v).isChecked();

        if(on)
        {
            //do something when toggle is on
            tView.setText("Toggle is on");
        }
        else
        {
            //do something when toggle is off
            tView.setText("Toggle is off");
        }
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}


2. ToggleButton OnClickListener

activity_main.xml

<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"
    android:padding="16dp"
    tools:context=".MainActivity"
    >
    <TextView
        android:id="@+id/tv"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="Click the ToggleButton"
        android:textColor="#006400"
        />
    <ToggleButton
        android:id="@+id/tb"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_below="@id/tv"
        />
</RelativeLayout>
MainActivity.java (Partial Code)

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

 //Get reference of the TextView from XML layout
 final TextView tView = (TextView) findViewById(R.id.tv);

 //Get reference of ToggleButton from XML layout
 ToggleButton tButton = (ToggleButton) findViewById(R.id.tb);

 //Set an OnClickListener for ToggleButton widget
 tButton.setOnClickListener(new OnClickListener() {
  @Override
  public void onClick(View v) {
   //Is the toggle is on?
   boolean on = ((ToggleButton) v).isChecked();

   if (on) {
    //do something when toggle is on
    tView.setText("Toggle is on");
   } else {
    //do something when toggle is off
    tView.setText("Toggle is off");
   }
  }
 });
}
Additional imported classes

import android.view.View;
import android.widget.ToggleButton;
import android.widget.TextView;
import android.view.View.OnClickListener;


3. ToggleButton OnCheckedChangeListener

MainActivity.java (Partial Code)

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

 //Get reference of the TextView from XML layout
 final TextView tView = (TextView) findViewById(R.id.tv);

 //Get reference of ToggleButton from XML layout
 ToggleButton tButton = (ToggleButton) findViewById(R.id.tb);

 //Set an OnCheckedChangeListener for ToggleButton
 tButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
  @Override
  public void onCheckedChanged(CompoundButton cb, boolean isChecked){
   if(isChecked)
   {
    //do something when toggle is on/checked
    tView.setText("ToggleButton is on/checked");
   }
   else
   {
    //do something when toggle is off/unchecked
    tView.setText("ToggleButton is off/unchecked");
   }
  }
 });
}
Additional imported classes

import android.widget.ToggleButton;
import android.widget.TextView;
import android.widget.CompoundButton;


More android examples