Android Switch Example

      No Comments on Android Switch Example

Android Switch Example

Android Switch Example

Android Switch is useful Control when you have on/off kind of situation. For example if you are building apps for alarm then you need on/off kind of button. You can use Switch. Android Switch is very easy to use. Its similar to Toggle Button or Checkbox but control will look different.

Location : res\layout

FileName: activity_main.xml

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="android.switchexample.MainActivity" >

    <Switch
        android:id="@+id/switch1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:checked="true"
        android:text="Switch" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="Wow... Its Working !"
        android:textColor="@color/blue"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="59dp"
        android:text="Developed By: Adarsh Patel"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>

Location : src\package\

FileName : MainActivity.java

package android.switchexample;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.Switch;
import android.widget.TextView;



public class MainActivity extends Activity {

	Switch s1;
	TextView tv1;
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        s1 = (Switch) findViewById(R.id.switch1);
        tv1 = (TextView) findViewById(R.id.textView1);
        
        s1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
				// TODO Auto-generated method stub
			
				if(arg1 == true)
				{
					tv1.setText("Wow... Its Working !");
					tv1.setTextColor(Color.BLUE);

				}
				else
				{
					tv1.setText("Sorry... Its Off ");
					tv1.setTextColor(Color.RED);
				}
			}
		});
        
        s1.setChecked(true);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

 

Output 1

Android Switch Example

Output 2

Android Switch Example

Download Code

[sociallocker]Click Here to Download Complete Code[/sociallocker]

Leave a Reply