Wednesday, 26 February 2014

How to Create SwitchCreation in Android

main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Switch"
        android:textColor="#339933"
        android:textSize="20sp"
        android:textStyle="bold"
        android:layout_gravity="center"
        android:layout_marginTop="40dp"/>
   
    <!-- Required minSdkVersion=14 in AndroidManifest.xml to use switch functionality -->
    <Switch
        android:id="@+id/switchCreation"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="150dp"/>

</LinearLayout>

SwitchCreationActivity,java

package com.anju.tiwari.switchcreation;

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

public class SwitchCreationActivity extends Activity {
Switch switchCreation;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        switchCreation=(Switch)findViewById(R.id.switchCreation);
        switchCreation.setOnCheckedChangeListener(new OnCheckedChangeListener() {
           
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                // TODO Auto-generated method stub
            if(isChecked==true){
                Toast.makeText(getApplicationContext(), "Switch ON", Toast.LENGTH_LONG).show();
            }   
            else{
                Toast.makeText(getApplicationContext(), "Switch OFF", Toast.LENGTH_LONG).show();
            }
            }
        });
   
    }

    }

AndroidManifest.xml


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.anju.tiwari.switchcreation"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="18" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:screenOrientation="portrait"
            android:name="com.anju.tiwari.switchcreation.SwitchCreationActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>