Checkbox:-
Check boxes are used when you want to use want to select one or more option from a set of alternative if only one option is to be selected at a time then you must select RadioButton
Select the Item in Checkbox :-
In this example. There four checkbox Tea,Snecks,Coffee,Pakodas
Whatever you choose the checkbox value is show in TextView.
Layout of program:-
Layout of Checkbox |
We make it by Graphical Method:By default in Graphical Method it show Icon and Text in pop down menu.We can can select Show only Icon.
Graphical Design of Checkbox problem |
Graphical and Properties view |
By drag and drop you make easily layout once you make the main.xml file auto generated
<?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/checkboxid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/example"
android:textColor="@color/green"
android:textSize="32dp" />
<CheckBox
android:id="@+id/teaid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/t"
android:textColor="@color/col2" />
<CheckBox
android:id="@+id/sneckid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/s"
android:textColor="@color/col3" />
<CheckBox
android:id="@+id/coffeeid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/c"
android:textColor="@color/col4" />
<CheckBox
android:id="@+id/pakodasid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/p"
android:textColor="@color/col5" />
<TextView
android:id="@+id/textView1"
android:layout_width="112dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textSize="16dp"
android:text="@string/distxt" />
</LinearLayout>
String.xml |
Android Resources(default) |
Explanation:
Here is
Hello and app_name is default String
Four checkbox label
Five color label
One Label Example String
One TextView
Now the Java Source Code
Java Source Code:-
package abh.checkbutton.com;
import android.app.Activity;
import android.os.Bundle;
import android.widget.*;
public class CheckbuttonexampleActivity extends Activity implements CompoundButton.OnCheckedChangeListener {
/** Called when the activity is first created. */
CheckBox te,sn,cof,pak;
TextView tv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
te=(CheckBox) findViewById(R.id.teaid);
sn=(CheckBox) findViewById(R.id.sneckid);
cof=(CheckBox) findViewById(R.id.coffeeid);
pak=(CheckBox) findViewById(R.id.pakodasid);
tv=(TextView) findViewById(R.id.textView1);
te.setOnCheckedChangeListener(this);
sn.setOnCheckedChangeListener(this);
cof.setOnCheckedChangeListener(this);
pak.setOnCheckedChangeListener(this);
}
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
String str=" ";
if(te.isChecked())
{
str=str+te.getText();
tv.setText(str);
}
if(sn.isChecked())
{
str=str+","+sn.getText();
tv.setText(str);
}
if(cof.isChecked())
{
str=str+","+cof.getText();
tv.setText(str);
}
if(pak.isChecked())
{
str=str+","+pak.getText();
tv.setText(str);
}
}
}
Expanation:-
We define OnCheckedChangeListener but we need to wire it to UI element before it come to action.There is two line of code that connect to checkbox to listen the checkbox click event
(1) Declaration of checkbox in main.xml file
<CheckBox
android:id="@+id/teaid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/t"
android:textColor="@color/col2" />
<CheckBox
android:id="@+id/sneckid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/s"
android:textColor="@color/col3" />
<CheckBox
android:id="@+id/coffeeid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/c"
android:textColor="@color/col4" />
<CheckBox
android:id="@+id/pakodasid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/p"
android:textColor="@color/col5" />
(2) te,sn,cof,pak,tv,te,te,sn,cof,pak is checkbox variable is connected to setOnCheckedChangeListener(this) by two line of code for each checkbox
te=(CheckBox) findViewById(R.id.teaid);
sn=(CheckBox) findViewById(R.id.sneckid);
cof=(CheckBox) findViewById(R.id.coffeeid);
pak=(CheckBox) findViewById(R.id.pakodasid);
tv=(TextView) findViewById(R.id.textView1);
te.setOnCheckedChangeListener(this);
sn.setOnCheckedChangeListener(this);
cof.setOnCheckedChangeListener(this);
pak.setOnCheckedChangeListener(this);
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
It is method which takes two parameter CompoundButton and boolean. checkedId is Id of the checkbox which is clicked.
tv.setText(str):-By this method you can set the value of checkbox in TextView.
This comment has been removed by the author.
ReplyDelete