Android CheckBox


1.Static CheckBox Creation in Android. 
2.Dynamic CheckBox Creation in Android. 
3.Reset CheckBox in Android.


1.Static CheckBox 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: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="Static CheckBox" />   
   <CheckBox   
     android:id="@+id/checkBox1"   
     android:layout_width="wrap_content"   
     android:layout_height="wrap_content"   
     android:layout_centerHorizontal="true"   
     android:layout_marginTop="110dp"   
     android:text="Hemanth" />   
   <CheckBox   
     android:id="@+id/checkBox2"   
     android:layout_width="wrap_content"   
     android:layout_height="wrap_content"   
     android:layout_centerHorizontal="true"   
     android:layout_marginTop="160dp"   
     android:text="Somaraju" />   
   <CheckBox   
     android:id="@+id/checkBox3"   
     android:layout_width="wrap_content"   
     android:layout_height="wrap_content"   
     android:layout_centerHorizontal="true"   
     android:layout_marginTop="210dp"   
     android:text="Kakinada" />   
   <Button   
     android:id="@+id/button1"   
     android:layout_width="wrap_content"   
     android:layout_height="wrap_content"   
     android:layout_centerHorizontal="true"   
     android:layout_marginTop="290dp"   
     android:text="Proceed" />   
 </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.View;   
 import android.widget.Button;   
 import android.widget.CheckBox;   
 import android.widget.Toast;   
 public class MainActivity extends Activity {   
 CheckBox c1, c2, c3;   
 Button b;   
   @Override   
   public void onCreate(Bundle savedInstanceState) {   
     super.onCreate(savedInstanceState);   
     setContentView(R.layout.activity_main);   
     c1 = (CheckBox) findViewById(R.id.checkBox1);   
     c2 = (CheckBox) findViewById(R.id.checkBox2);   
     c3 = (CheckBox) findViewById(R.id.checkBox3);   
     b = (Button) findViewById(R.id.button1);   
     b.setOnClickListener(new View.OnClickListener() {   
 public void onClick(View v) {   
 // TODO Auto-generated method stub   
 if((c1.isChecked()==false) && (c2.isChecked()==false) && (c3.isChecked()==false))   
 {   
 Toast.makeText(getBaseContext(),"None Selected",   
 Toast.LENGTH_SHORT).show();   
 }   
 else   
 {   
 String str = null;   
 if(c1.isChecked())   
 {   
 str="Hemanth";   
 }   
 if(c2.isChecked())   
 {   
 str=str.concat("Somaraju");   
 }   
 if(c3.isChecked())   
 {   
 str=str.concat("Kakinada");   
 }   
 int i=str.length();   
 String str1=" are selected";   
 String str2=" is selected";   
 if(i>9)   
 {   
 str=str.concat(str1);   
 Toast.makeText(getBaseContext(), str,   
 Toast.LENGTH_SHORT).show();   
 }   
 else   
 {   
 str=str.concat(str2);   
 Toast.makeText(getBaseContext(), str,   
 Toast.LENGTH_SHORT).show();   
 }   
 }   
 }   
 });   
     b.setOnLongClickListener(new View.OnLongClickListener() {   
 public boolean onLongClick(View v) {   
 // TODO Auto-generated method stub   
 c1.setChecked(false);   
 c2.setChecked(false);   
 c3.setChecked(false);   
 Toast.makeText(getBaseContext(), "Long Pressed & Refreshed ChekBoxes", Toast.LENGTH_SHORT).show();   
 return true;   
 }   
 });   
   }   
   @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="19" />   
   <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 
  
Image 
 Image 
  

2.Dynamic CheckBox 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 CheckBox" />   
 </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.View;   
 import android.view.ViewGroup.LayoutParams;   
 import android.widget.Button;   
 import android.widget.CheckBox;   
 import android.widget.RelativeLayout;   
 import android.widget.Toast;   
 public class MainActivity extends Activity {   
 RelativeLayout rl;   
 CheckBox c1, c2, c3;   
 Button b;   
   @Override   
   public void onCreate(Bundle savedInstanceState) {   
     super.onCreate(savedInstanceState);   
     setContentView(R.layout.activity_main);   
     rl = (RelativeLayout) findViewById(R.id.rl);   
     c1 = new CheckBox (MainActivity.this);   
     c2 = new CheckBox (MainActivity.this);   
     c3 = new CheckBox (MainActivity.this);   
     b = new Button (MainActivity.this);   
     c1.setText("Hemanth");   
     c2.setText("Somaraju");   
     c3.setText("Kakinada");   
     b.setText("Proceed");   
     RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams   
      ((int)LayoutParams.WRAP_CONTENT, (int) LayoutParams.WRAP_CONTENT);   
     params1.leftMargin = 150;   
     params1.topMargin = 170;   
     RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams   
      ((int)LayoutParams.WRAP_CONTENT, (int) LayoutParams.WRAP_CONTENT);   
     params2.leftMargin = 150;   
     params2.topMargin = 235;   
     RelativeLayout.LayoutParams params3 = new RelativeLayout.LayoutParams   
      ((int)LayoutParams.WRAP_CONTENT, (int) LayoutParams.WRAP_CONTENT);   
     params3.leftMargin = 150;   
     params3.topMargin = 300;   
     RelativeLayout.LayoutParams params4 = new RelativeLayout.LayoutParams   
      ((int)LayoutParams.WRAP_CONTENT, (int) LayoutParams.WRAP_CONTENT);   
     params4.leftMargin = 150;   
     params4.topMargin = 400;   
     c1.setLayoutParams(params1);   
     c2.setLayoutParams(params2);   
     c3.setLayoutParams(params3);   
     b.setLayoutParams(params4);   
     rl.addView(c1);   
     rl.addView(c2);   
     rl.addView(c3);   
     rl.addView(b);   
     b.setOnClickListener(new View.OnClickListener() {   
 public void onClick(View v) {   
 // TODO Auto-generated method stub   
 if((c1.isChecked()==false) && (c2.isChecked()==false) && (c3.isChecked()==false))   
 {   
 Toast.makeText(getBaseContext(),"None Selected",   
 Toast.LENGTH_SHORT).show();   
 }   
 else   
 {   
 String str = null;   
 if(c1.isChecked())   
 {   
 str="Hemanth";   
 }   
 if(c2.isChecked())   
 {   
 str=str.concat("Somaraju");   
 }   
 if(c3.isChecked())   
 {   
 str=str.concat("Kakinada");   
 }   
 int i=str.length();   
 String str1=" are selected";   
 String str2=" is selected";   
 if(i>9)   
 {   
 str=str.concat(str1);   
 Toast.makeText(getBaseContext(), str,   
 Toast.LENGTH_SHORT).show();   
 }   
 else   
 {   
 str=str.concat(str2);   
 Toast.makeText(getBaseContext(), str,   
 Toast.LENGTH_SHORT).show();   
 }   
 }   
 }   
 });   
     b.setOnLongClickListener(new View.OnLongClickListener() {   
 public boolean onLongClick(View v) {   
 // TODO Auto-generated method stub   
 c1.setChecked(false);   
 c2.setChecked(false);   
 c3.setChecked(false);   
 Toast.makeText(getBaseContext(), "Long Pressed & Refreshed ChekBoxes", Toast.LENGTH_SHORT).show();   
 return true;   
 }   
 });   
   }   
   @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="19" />   
   <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 
  
Image 
 Image 

3.Reset CheckBox 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:textSize="20dp"   
     android:layout_width="wrap_content"   
     android:layout_height="wrap_content"   
     android:layout_alignParentTop="true"   
     android:layout_centerHorizontal="true"   
     android:layout_marginTop="50dp"   
     android:text="Reset CheckBox" />   
   <CheckBox   
     android:id="@+id/checkBox1"   
     android:layout_width="wrap_content"   
     android:layout_height="wrap_content"   
     android:layout_alignParentTop="true"   
     android:layout_centerHorizontal="true"   
     android:layout_marginTop="120dp"   
     android:text="Hemanth" />   
   <CheckBox   
     android:id="@+id/checkBox2"   
     android:layout_width="wrap_content"   
     android:layout_height="wrap_content"   
     android:layout_centerHorizontal="true"   
     android:layout_below="@+id/checkBox1"   
     android:layout_marginTop="40dp"   
     android:text="Somaraju" />   
   <CheckBox   
     android:id="@+id/checkBox3"   
     android:layout_width="wrap_content"   
     android:layout_height="wrap_content"   
     android:layout_centerHorizontal="true"   
     android:layout_below="@+id/checkBox2"   
     android:layout_marginTop="40dp"   
     android:text="Kakinada" />   
   <Button   
     android:id="@+id/button1"   
     android:layout_width="wrap_content"   
     android:layout_height="wrap_content"   
     android:layout_below="@+id/checkBox3"   
     android:layout_centerHorizontal="true"   
     android:layout_marginTop="40dp"   
     android:text="Reset" />   
 </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.View;   
 import android.widget.Button;   
 import android.widget.CheckBox;   
 public class MainActivity extends Activity {   
 CheckBox c1, c2, c3;   
 Button reset;   
   @Override   
   public void onCreate(Bundle savedInstanceState) {   
     super.onCreate(savedInstanceState);   
     setContentView(R.layout.activity_main);   
     c1 = (CheckBox) findViewById(R.id.checkBox1);   
     c2 = (CheckBox) findViewById(R.id.checkBox2);   
     c3 = (CheckBox) findViewById(R.id.checkBox3);   
     reset = (Button) findViewById(R.id.button1);   
     reset.setOnClickListener(new View.OnClickListener() {   
 @Override   
 public void onClick(View v) {   
 // TODO Auto-generated method stub   
 c1.setChecked(false);   
 c2.setChecked(false);   
 c3.setChecked(false);   
 }   
 });   
   }   
   @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="19" />   
   <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 6 : Our output will be like this : 
  
  
Image 
 Image