Android CheckBox Example

By | July 21, 2013
In previous tutorial we have discussed about Radio button which allows users to select one options at a time but what if user wants to select more than one option at a time ? this can achieved  by using android CheckBox widget , android provides “android.widget.CheckBox” class for rendering a CheckBox .

CheckBox allows us to select more than one options at a time and checkbox has got two types  of state they are checked and unchecked


Lets see an example 

Project Detail

Project Name checkbox
Package com.example.checkbox
Minimum SDK API 8
Target SDK API 17
Theme Holo Light with Dark Action Bar

1. XML Layout

file:- activity_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:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Which is your Fav Sports"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <CheckBox
        android:id="@+id/chk_cri"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Cricket"  />
 

    <CheckBox
        android:id="@+id/chk_food"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Foodball"  />
 
    <CheckBox
        android:id="@+id/chk_bas"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="BasketBall"  />
 
    <Button
        android:id="@+id/show"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:text="Show" />

    <TextView
        android:id="@+id/display"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:text=""
        android:textAppearance="?android:attr/textAppearanceMedium" />

</LinearLayout>

2. Activity

file:- MainActvity.java

package com.example.checkbox;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;

public class MainActivity extends Activity {

CheckBox chk_cri, chk_food, chk_bas;
TextView display;
Button show;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// Initalize the views
chk_cri = (CheckBox) findViewById(R.id.chk_cri);
chk_food = (CheckBox) findViewById(R.id.chk_food);
chk_bas = (CheckBox) findViewById(R.id.chk_bas);
display = (TextView) findViewById(R.id.display);
show = (Button) findViewById(R.id.show);

// set onlcicklistener for button show
show.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
display.setText("");

if (chk_cri.isChecked()) {
displaymsg("Cricket");
}

if (chk_food.isChecked()) {
displaymsg("foodball");
}
if (chk_bas.isChecked()) {
displaymsg("basketball");
}
}
});
}

private void displaymsg(String msg) {

display.setText(display.getText() + "n" + msg);
}
}

3. RUN

Android CheckBox ExampleAndroid CheckBox Example

Android CheckBox Example Code