Table of Contents
Android Switch Button is UI widget which has only two states i.e. ON and OFF. It allows user to change setting between these two states.
Android Switch button is available only in Android 4.0 or later android devices.
Example :
You can use these buttons to change On and Off state of WiFi or Bluetooth
Source code:
Step 1 :Creating Project
Create an android application project named “SwitchButtonExampleApp”.
Step 2 : Creating Layout
Change res ->layout -> activity_main.xml as below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" 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" android:gravity="center_horizontal" tools:context="com.java2blog.switchbuttonexampleapp.MainActivity"> <Switch android:id="@+id/switch1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Switch : " > </Switch> </RelativeLayout> |
Step 3 : Creating MainActivity
Change src/main/packageName/MainActivity.java as below:
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 |
package com.java2blog.switchbuttonexampleapp; import android.app.Activity; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Switch; import android.widget.Toast; import android.view.View.OnClickListener; public class MainActivity extends AppCompatActivity { Switch switchButton; private Activity activity; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); activity = this; switchButton = (Switch) findViewById(R.id.switch1); switchButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if (switchButton.isChecked()) { Toast.makeText(activity, "Switch is in ON State", Toast.LENGTH_LONG).show(); } else { Toast.makeText(activity, "Switch is in OFF State", Toast.LENGTH_LONG).show(); } } }); } } |
We are getting Switch reference from layout file and then using Switch setOnClickListener to register event for state change.
Switch’s isChecked() method is used to know current state of Switch whether it is on state or off state.
Switch’s isChecked() method is used to know current state of Switch whether it is on state or off state.
Step 4 : Running the app
When you run the app, you will get below screen:
When you click on switch button, it will go to on state.
When you again click on switch button, it will go to off state.
Was this post helpful?
Let us know if this post was helpful. Feedbacks are monitored on daily basis. Please do provide feedback as that\'s the only way to improve.