How to use Checkbox in android?

Leave a Comment

What is a CheckBox ?
Checkboxes are the GUI elements which provide facility to select multiple options (from zeroto all) from an available list of options. It is very useful while developing any kind of application and Android is not an exception. A CheckBox can have two states i.e, eitherchecked or Unchecked . We can make our logic according to these two states of a checkbox. In general, it can be said that it is not much different from any HTML input(<input type=”checkbox” name=”name1″ value=”value1″ />), ASP.NET checkbox(<asp:CheckBox ID=”CheckBox1″ runat=”server” >) etc. with very few differences.

CheckBox in Android :

To demonstrate a very simple example using checkbox,  I have dragged a TextView and two CheckBox to my main.xml and did the basic setting-up kind of things like id,text,textColor,background,width,height etc as per my need.
Now create a OnClickListener which will later listen to our on-click events on the two checkboxes. Then put a very simple logic in the onClick method to show text of the checked checkbox(s) in the TextView and let reset the text of the TextView if none of these two checkboxes are checked.

 main.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:orientation="vertical" >

<TextView
 android:id="@+id/tvDetails"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:layout_weight="0.17"
 android:textSize="22dp"
 android:background="@android:color/white"
 android:textColor="@android:color/black" />

<CheckBox
 android:id="@+id/cbSuvendu"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:text="@string/suvendu" />

<CheckBox
 android:id="@+id/cbWordPress"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:text="@string/wordpress" />

</LinearLayout>

String.xml

<?xml version="1.0" encoding="utf-8"?>
 <resources>

<string name="hello">Hello World, CheckBoxTutorialActivity!</string>
 <string name="app_name">First CheckBox Tutorial</string>
 <string name="jackk">jiteshandroidlife.blogspot.in</string>
 <string name="wordpress">jiteshdalsniya.wordpress.com</string>

</resources>

Activity.java

package com.jackk.tutorial.cb;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.TextView;

public class CheckBoxTutorialActivity extends Activity {
 TextView tv;
 CheckBox cbS;
 CheckBox cbW;
 OnClickListener checkBoxListener;
 @Override
 public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.main);
 cbS=(CheckBox)findViewById(R.id.cbSuvendu);
 cbW=(CheckBox)findViewById(R.id.cbWordPress);
 checkBoxListener =new OnClickListener() {

 @Override
 public void onClick(View v) {
 tv=(TextView)findViewById(R.id.tvDetails);
 tv.setText("I Like ");
 if(cbS.isChecked())
 {
 tv.setText(tv.getText().toString()+" "+ cbS.getText().toString());
 }
 if(cbW.isChecked())
 {
 tv.setText(tv.getText().toString()+ " "+cbW.getText().toString());
 }
 if(!cbS.isChecked()&&!cbW.isChecked())
 {
 tv.setText("");
 }
 }
 };

 cbS.setOnClickListener(checkBoxListener);
 cbW.setOnClickListener(checkBoxListener);

 }
}
Snaps










0 comments:

Post a Comment