Switches Example In Android

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"?-->
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">

android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="32dip" />

android:checked="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="32dip" />

android:text="Monitored switch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="32dip" />

android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOn="Start"
android:textOff="Stop"
android:layout_marginBottom="32dip" />

 

 

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

Leave a Comment:

5 comments
krishnakiran says June 30, 2012

in my xml der is no option switch ,,,available options are imageswitcher,textswitcher,viewswitcher ,,can u tell me which one i have to use for d above switchesexample,,,,help me out please…

Reply
VijayaDurga says December 12, 2012

Sir Please suggest me , how to place our app into google store.

Reply
appstore says January 11, 2013

I’ve been exploring for a little for any high-quality articles or weblog posts on this sort of area . Exploring in Yahoo I eventually stumbled upon this web site. Reading this information So i’m satisfied to show that I have a very excellent uncanny feeling I discovered just what I needed. I such a lot surely will make sure to do not fail to remember this site and give it a glance regularly.

Reply
android apps says March 28, 2013

Somebody essentially assist to make critically posts I would state. That is the very first time I frequented your website page and to this point? I surprised with the analysis you made to create this particular put up extraordinary. Great activity!

Reply
Bob says July 2, 2013

Doesn’t work. Compiles but breaks when ran

Reply
Add Your Reply