
A Switch is a two-state button through which only two states ON and OFF can be selected and it is one of the most use android user interface (UI) component in application setting. Switch button is only supported in android 4.0 and larger device. Following are the simple steps to implement Switch button in android.
Android Example: How to use Switch Button in Android
XML Layout File
res/layout/switch_button_example.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margin="16dp" android:gravity="center_horizontal" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Android Switch Button Example" /> <Switch android:id="@+id/switchButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="50dp" android:text="Android Switch Button 1" /> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Android Switch Button" /> <Switch android:id="@+id/switchButton2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="30dp" android:text="Android Switch Button 2" /> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Android Switch Button" /> <TextView android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center|bottom" android:text="ViralAndroid.com" android:textSize="24sp" android:textStyle="bold" /> </LinearLayout> |
Java Activity File
src/MainActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
package com.androindian; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.widget.CompoundButton; import android.widget.Switch; import android.widget.TextView; public class MainActivity extends AppCompatActivity { Switch switchButton, switchButton2; TextView textView, textView2; String switchOn = "Switch is ON"; String switchOff = "Switch is OFF"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.switch_button_example); // For first switch button switchButton = (Switch) findViewById(R.id.switchButton); textView = (TextView) findViewById(R.id.textView); switchButton.setChecked(true); switchButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton compoundButton, boolean bChecked) { if (bChecked) { textView.setText(switchOn); } else { textView.setText(switchOff); } } }); if (switchButton.isChecked()) { textView.setText(switchOn); } else { textView.setText(switchOff); } // for second switch button switchButton2 = (Switch) findViewById(R.id.switchButton2); textView2 = (TextView) findViewById(R.id.textView2); switchButton2.setChecked(false); switchButton2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton compoundButton, boolean bChecked) { if (bChecked) { textView2.setText(switchOn); } else { textView2.setText(switchOff); } } }); if (switchButton2.isChecked()) { textView2.setText(switchOn); } else { textView2.setText(switchOff); } } } |