Gallery

Switches Example in Android


Posted by Sushant on June 21, 2012, filed in: Android Apps, ANDROID SDK TUTORIAL, ANDROID TUTORIAL

This example shows how to create and set switches in android.

Algorithm:

1.) Create a new project by File-> New -> Android Project name it SwitchesExample.

2.) Write following into main.xml:

<?xml version="1.0" encoding="utf-8"?>
 <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
     <LinearLayout
         android:orientation="vertical"
         android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <Switch android:text="Standard switch"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginBottom="32dip" />
        <Switch android:text="Default is on"
                android:checked="true"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginBottom="32dip" />
        <Switch android:id="@+id/monitored_switch"
                android:text="Monitored switch"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginBottom="32dip" />
        <Switch android:text="Customized text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textOn="Start"
                android:textOff="Stop"
                android:layout_marginBottom="32dip" />       
     </LinearLayout>
 </ScrollView>

3.) Run for output.

Steps:

1.) Create a project named SwitchesExample and set the information as stated in the image.

Build Target: Android 4.0
Application Name: SwitchesExample
Package Name: com. example. SwitchesExample
Activity Name: SwitchesExample
Min SDK Version: 14

2.) Open SwitchesExample.java file and write following code there:

package com.example.SwitchesExample;

import android.app.Activity;
 import android.os.Bundle;
 import android.widget.CompoundButton;
 import android.widget.Switch;
 import android.widget.Toast;
public class SwitchesExample extends Activity implements CompoundButton.OnCheckedChangeListener {
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);

         setContentView(R.layout.main);

         Switch s = (Switch) findViewById(R.id.monitored_switch);
         if (s != null) {
             s.setOnCheckedChangeListener(this);
         }
     }
    @Override
     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        Toast.makeText(this, "Monitored switch is " + (isChecked ? "on" : "off"),
                Toast.LENGTH_SHORT).show();
     }
 }
3.) Compile and build the project.

Output

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s