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:
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:
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>
<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
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
0 comments
Thanks for your comment