Android Checkbox Example

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"


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>






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.

Comments

  1. I lately got here helped here all through your blog and were dissecting nearby. I figured I would leave my most fundamental remark. I dont see what to articulate except for that I have treasured checking out.. Pc Reviver licence Code

    ReplyDelete
  2. Cultivating Simulator most recent variant is accommodates the client many astonishing highlights and extremely simple to use for the every client.Farming Simulator Free Download

    ReplyDelete
  3. When I read this story, I was so happy. Thank you for writing such a great article.
    https://getcrackpc.com/drivermax-pro-free-download/

    ReplyDelete

Post a Comment

Popular posts from this blog

Privacy Policy For apps

Android Development tips for Beginners