Android Toggle Button


1.Static Toggle Button Creation in Android. 
2.Dynamic Toggle Button Creation in Android. 
  
1.Static Toggle Button Creation in Android. 
Step 1 : Select File -> New -> Project -> Android Application Project (or) Android Project. Fill the forms and click "Finish" button. If you have any doubt regarding create a new project Click Here. 
   
Step 2 : Open res -> layout -> activity_main.xml (or) main.xml and add following code: 

 <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" >   
   <TextView   
     android:id="@+id/textView1"   
     android:layout_width="wrap_content"   
     android:layout_height="wrap_content"   
     android:layout_alignParentTop="true"   
     android:layout_centerHorizontal="true"   
     android:layout_marginTop="60dp"   
     android:text="Static ToggleButton By Hemanth Somaraju"   
     android:textColor="#FF0000"   
     android:textSize="20dp" />   
   <ToggleButton   
     android:id="@+id/toggleButton1"   
     android:layout_width="wrap_content"   
     android:layout_height="wrap_content"   
     android:layout_below="@+id/textView1"   
     android:layout_centerHorizontal="true"   
     android:layout_marginTop="76dp"   
     android:text="ToggleButton" />   
 </RelativeLayout>

Step 3 : Open src -> package -> MainActivity.java and add following code :  

 package com.gudivada.hemanthsomaraju;   
 import android.os.Bundle;   
 import android.app.Activity;   
 import android.view.Menu;   
 import android.widget.CompoundButton;   
 import android.widget.Toast;   
 import android.widget.CompoundButton.OnCheckedChangeListener;   
 import android.widget.ToggleButton;   
 public class MainActivity extends Activity {   
 ToggleButton tb;   
   @Override   
   public void onCreate(Bundle savedInstanceState) {   
     super.onCreate(savedInstanceState);   
     setContentView(R.layout.activity_main);   
     tb = (ToggleButton) findViewById(R.id.toggleButton1);   
     tb.setOnCheckedChangeListener(new OnCheckedChangeListener() {   
 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {   
 // TODO Auto-generated method stub   
 if(isChecked == true)   
 {   
 Toast.makeText(getBaseContext(), "ON State",   
 Toast.LENGTH_SHORT).show();   
 }   
 else   
 {   
 Toast.makeText(getBaseContext(), "OFF State",   
 Toast.LENGTH_SHORT).show();   
 }   
 }   
 });   
   }   
   @Override   
   public boolean onCreateOptionsMenu(Menu menu) {   
     getMenuInflater().inflate(R.menu.main, menu);   
     return true;   
   }   
 } 

Step 4 : Open AndroidManifest.xml and add following code : 

 <?xml version="1.0" encoding="utf-8"?>   
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"   
   package="com.gudivada.hemanthsomaraju"   
   android:versionCode="1"   
   android:versionName="1.0" >   
   <uses-sdk   
     android:minSdkVersion="8"   
     android:targetSdkVersion="18" />   
   <application   
     android:allowBackup="true"   
     android:icon="@drawable/ic_launcher"   
     android:label="@string/app_name"   
     android:theme="@style/AppTheme" >   
     <activity   
       android:name="com.gudivada.hemanthsomaraju.MainActivity"   
       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> 

Step 5 : Our output will be like this :  
  
Image 
 Image 
  

2.Dynamic Toggle Button Creation in Android. 
Step 1 : Select File -> New -> Project -> Android Application Project (or) Android Project. Fill the forms and click "Finish" button. If you have any doubt regarding create a new project Click Here. 
   
Step 2 : Open res -> layout -> activity_main.xml (or) main.xml and add following code: 

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"   
   xmlns:tools="http://schemas.android.com/tools"   
   android:id="@+id/rl"   
   android:layout_width="match_parent"   
   android:layout_height="match_parent" >   
   <TextView   
     android:id="@+id/textView1"   
     android:textColor="#FF0000"   
     android:textSize="20dp"   
     android:layout_width="wrap_content"   
     android:layout_height="wrap_content"   
     android:layout_alignParentTop="true"   
     android:layout_centerHorizontal="true"   
     android:layout_marginTop="60dp"   
     android:text="Dynamic ToggleButton By Hemanth Somaraju" />   
 </RelativeLayout>  

Step 3 : Open src -> package -> MainActivity.java and add following code :  

 package com.gudivada.hemanthsomaraju;   
 import android.os.Bundle;   
 import android.app.Activity;   
 import android.view.Menu;   
 import android.view.ViewGroup.LayoutParams;   
 import android.widget.CompoundButton;   
 import android.widget.CompoundButton.OnCheckedChangeListener;   
 import android.widget.RelativeLayout;   
 import android.widget.Toast;   
 import android.widget.ToggleButton;   
 public class MainActivity extends Activity {   
 RelativeLayout rl;   
 ToggleButton tb1;   
   @Override   
   public void onCreate(Bundle savedInstanceState) {   
     super.onCreate(savedInstanceState);   
     setContentView(R.layout.activity_main);   
     rl = (RelativeLayout) findViewById(R.id.rl);   
     tb1 = new ToggleButton (MainActivity.this);   
     RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams   
      ((int)LayoutParams.WRAP_CONTENT, (int) LayoutParams.WRAP_CONTENT);   
     params.leftMargin = 180;   
     params.topMargin = 200;   
     tb1.setLayoutParams(params);   
     tb1.setOnCheckedChangeListener(new OnCheckedChangeListener() {   
 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {   
 // TODO Auto-generated method stub   
 if(isChecked == true)   
 {   
 Toast.makeText(getBaseContext(), "ON State",   
 Toast.LENGTH_SHORT).show();   
 }   
 else   
 {   
 Toast.makeText(getBaseContext(), "OFF State",   
 Toast.LENGTH_SHORT).show();   
 }   
 }   
 });   
     rl.addView(tb1);   
   }   
   @Override   
   public boolean onCreateOptionsMenu(Menu menu) {   
     getMenuInflater().inflate(R.menu.main, menu);   
     return true;   
   }   
 }   

Step 4 : Open AndroidManifest.xml and add following code : 

 <?xml version="1.0" encoding="utf-8"?>   
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"   
   package="com.gudivada.hemanthsomaraju"   
   android:versionCode="1"   
   android:versionName="1.0" >   
   <uses-sdk   
     android:minSdkVersion="8"   
     android:targetSdkVersion="18" />   
   <application   
     android:allowBackup="true"   
     android:icon="@drawable/ic_launcher"   
     android:label="@string/app_name"   
     android:theme="@style/AppTheme" >   
     <activity   
       android:name="com.gudivada.hemanthsomaraju.MainActivity"   
       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>   

Step 5 : Our output will be like this :  
  
Image 
 Image