A checkbox is a specific type of two-states button that can be either checked or unchecked.
We will learn how to use checkbox in this tutorial.
Note: You should know how to use Buttons and TextViews , if you do not know then read this post Using Buttons and TextViews in Android.
Create a new project.
Name your activity "CheckBoxActivity"
open main.xml file and add TextViews, CheckBoxes and Buttons, it should like like below. (you can just copy the below code and paste in main.xml file)
<LinearLayout 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:orientation="vertical" >
<TextView
android:text="Choose Your Hobies"
android:layout_marginTop="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<CheckBox
android:id="@+id/chkReading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Reading" />
<CheckBox
android:id="@+id/chkSong"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Listening Songs"
android:checked="true" />
<CheckBox
android:id="@+id/chkPLay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Playing" />
<CheckBox
android:id="@+id/chkSing"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Singing" />
<CheckBox
android:id="@+id/chkDance"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Dancing" />
<TextView
android:id="@+id/result"
android:textSize="20dp"
android:layout_marginTop="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/btnDisplay"
android:layout_marginTop="20dp"
android:gravity="center_horizontal"
android:layout_gravity="center_horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" OK " />
</LinearLayout>
We will learn how to use checkbox in this tutorial.
Note: You should know how to use Buttons and TextViews , if you do not know then read this post Using Buttons and TextViews in Android.
Create a new project.
Name your activity "CheckBoxActivity"
Editing main.xml file :
open main.xml file and add TextViews, CheckBoxes and Buttons, it should like like below. (you can just copy the below code and paste in main.xml file)
main.xml
<LinearLayout 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:orientation="vertical" >
<TextView
android:text="Choose Your Hobies"
android:layout_marginTop="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<CheckBox
android:id="@+id/chkReading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Reading" />
<CheckBox
android:id="@+id/chkSong"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Listening Songs"
android:checked="true" />
<CheckBox
android:id="@+id/chkPLay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Playing" />
<CheckBox
android:id="@+id/chkSing"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Singing" />
<CheckBox
android:id="@+id/chkDance"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Dancing" />
<TextView
android:id="@+id/result"
android:textSize="20dp"
android:layout_marginTop="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/btnDisplay"
android:layout_marginTop="20dp"
android:gravity="center_horizontal"
android:layout_gravity="center_horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" OK " />
</LinearLayout>
Thing To Note
android:checked="true" you can initially checked any check box using this tag. see in fig.
" Listening Song" is already checked.
Editing CheckBoxActivity file
CheckBoxActivity.java
public class CheckBoxActivity extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final TextView textHobbies=(TextView)findViewById(R.id.result);
// getting refference of check boxes
final CheckBox reading=(CheckBox)findViewById(R.id.chkReading);
final CheckBox playing=(CheckBox)findViewById(R.id.chkReading);
final CheckBox listening=(CheckBox)findViewById(R.id.chkPLay);
final CheckBox singing=(CheckBox)findViewById(R.id.chkSing);
final CheckBox dancing=(CheckBox)findViewById(R.id.chkDance);
// get reffrence of Button
Button button=(Button)findViewById(R.id.btnDisplay);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
String hobbies="";
if(reading.isChecked())
hobbies+="Reading";
if(playing.isChecked())
hobbies+=" Playing";
if(listening.isChecked())
hobbies+=" Listening Music";
if(singing.isChecked())
hobbies+=" Singing";
if(dancing.isChecked())
hobbies+=" Dancing";
textHobbies.setText("Your Hobbies are : "+hobbies) ;
}
});
}
}
Point to Note:
checkBoxObject.isChecked();
isChecked() method returns true if the checkBox is checked , and false otherwise.
Now run your project , do some activity(check or unceck checkboxes)
You will see the following.
Hope you learned and enjoyed
Comments and questions are welcome
Hello sir, your tutorial is great. but i have one query that i have arraylist which i have populated into listview and one customadapter class where using getview method i set. i.e eachrow has one textview and checkbox. i want to do when checkbox is clicked or checked corresponding textview is removed.i.e list.remove(position) so, how can i get checkbox position into listview . thanks.
ReplyDeletehow to pass the above result to another activity
ReplyDeleteMua vé máy bay tại Aivivu, tham khảo
ReplyDeletevé máy bay đi Mỹ
giá vé máy bay mỹ về việt nam
khi nào có chuyến bay từ đức về việt nam
giá vé máy bay từ nga về việt nam
vé máy bay từ anh về việt nam
chuyến bay từ pháp về việt nam
danh sách khách sạn cách ly tại quảng ninh
chuyến bay chuyên gia