Sunday, April 5, 2015

How to use On Off Switch Button in Android

1. Android On Off Switch Button 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="On off the switch..."
        android:textColor="#191970"
        />
    <Switch
        android:id="@+id/switch_btn"
        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 widgets reference from XML layout
 final TextView tView = (TextView) findViewById(R.id.tv);
 Switch sButton = (Switch) findViewById(R.id.switch_btn);

 //Set a CheckedChange Listener for Switch Button
 sButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
  @Override
  public void onCheckedChanged(CompoundButton cb, boolean on){
   if(on)
   {
    //Do something when Switch button is on/checked
    tView.setText("Switch is on.....");
   }
   else
   {
    //Do something when Switch is off/unchecked
    tView.setText("Switch is off.....");
   }
  }
 });
}
Additional imported classes

import android.widget.TextView;
import android.widget.Switch;
import android.widget.CompoundButton;

2. Switch Button Click Event

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 switch button..."
        />
    <Switch
        android:id="@+id/sw"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:onClick="onSwitchClicked"
        android:layout_below="@id/tv"
        />
</RelativeLayout>
MainActivity.java (Partial code)

//Response to the Switch button click event
public void onSwitchClicked(View v){
 //Get reference of TextView from XML layout
 TextView tView = (TextView) findViewById(R.id.tv);

 //Is the switch on?
 boolean on = ((Switch) v).isChecked();

 if(on)
 {
  //Do something when switch is on
  tView.setText("Switch is on....");
 }
 else
 {
  //Do something when switch is off
  tView.setText("Switch is off....");
 }
}
Additional imported classes

import android.widget.TextView;
import android.widget.Switch;
import android.view.View;

3. Switch Button Click Listener


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 switch button..."
        android:textColor="#6B8E23"
        />
    <Switch
        android:id="@+id/switch_btn"
        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 widgets reference from XML layout
 final TextView tView = (TextView) findViewById(R.id.tv);
 Switch sButton = (Switch) findViewById(R.id.switch_btn);

 //Set a Click Listener for Switch Button
 sButton.setOnClickListener(new OnClickListener(){
    @Override
  public void onClick(View v){
   //Is the switch is on?
     boolean on = ((Switch) v).isChecked();
     if(on)
     {
      //Do something when switch is on/checked
      tView.setText("Switch is on....");
     }
     else
     {
      //Do something when switch is off/unchecked
      tView.setText("Switch is off....");
     }
    }
 });
}
Additional imported classes

import android.widget.TextView;
import android.widget.Switch;
import android.view.View;
import android.view.View.OnClickListener;
More android examples