Android Projects

Android Development Books

Monday

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"?>
<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







More Details about this article:- http://www.edumobile.org


Share this post
  • Share to Facebook
  • Share to Twitter
  • Share to Google+
  • Share to Stumble Upon
  • Share to Evernote
  • Share to Blogger
  • Share to Email
  • Share to Yahoo Messenger
  • More...

0 comments

Thanks for your comment

:) :-) :)) =)) :( :-( :(( :d :-d @-) :p :o :>) (o) [-( :-? (p) :-s (m) 8-) :-t :-b b-( :-# =p~ :-$ (b) (f) x-) (k) (h) (c) cheer

Related Posts Plugin for WordPress, Blogger...
© Google Android Lovers
Designed by BlogThietKe Cooperated with Duy Pham
Released under Creative Commons 3.0 CC BY-NC 3.0