Download Android Switch Button Example - Android
Categories:Viewed: 142 - Published at: 2 years ago

Android Switch Button Example

Here you will get android switch button example.

Switch is a button which has two states, on or off. The user can on or off by just dragging or tapping finger on the button. This UI widget is supported on android version 4.0 and higher.

We can define switch button in XML layout by using tag.

The OnCheckedChangeListener is applied on button to get notified whenever its state is changed.

The isChecked() method is used to check the current state of switch. It returns true if state is ON else returns false.

You can set the initial state of switch by setChecked() method in following way.

//set switch button to ON
sButton.setChecked(true);

//set switch button to OFF
sButton.setChecked(false);

Android Switch Button Example

activity_main.xml

<?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:orientation="vertical"
    android:gravity="center">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:id="@+id/text1"
        android:layout_marginBottom="10dp"
        android:text="Play With Me..."/>

    <Switch
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/sButton"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:id="@+id/text2"
        android:layout_marginTop="10dp"/>
</LinearLayout>

MainActivity.java

package thecrazyprogrammer.androidexample;

import android.app.Activity;
import android.os.Bundle;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
    Switch sButton;
    TextView text2;

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

        sButton = (Switch)findViewById(R.id.sButton);
        text2 = (TextView)findViewById(R.id.text2);

        if(sButton.isChecked()){
            text2.setText("Switch Button is On");
        }
        else{
            text2.setText("Switch Button is Off");
        }

        sButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked == true){
                    Toast.makeText(MainActivity.this,"On",Toast.LENGTH_SHORT).show();
                    text2.setText("Switch Button is On");
                }
                else{
                    Toast.makeText(MainActivity.this,"Off",Toast.LENGTH_SHORT).show();
                    text2.setText("Switch Button is Off");
                }
            }
        });
    }
}

Screenshot

Android Switch Button Example

Comment below if you have any queries related to above android switch button example.

Reference: thecrazyprogrammer.com

TAGS :