Wednesday, 29 May 2013

Android Checkbox Example

In Android, you can use “android.widget.CheckBox” class to render a checkbox.

Checkboxes allow the user to select one or more options from a set. Typically, you should present each checkbox option in a vertical list.

To create each checkbox option, create a CheckBox in your layout. Because a set of checkbox options allows the user to select multiple items, each checkbox is managed separately and you must register a click listener for each one.

In this tutorial, we show you how to create  checkboxes in XML file, and demonstrates the use of listener to check the checkbox state – checked or unchecked.

2. CheckBox

<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"
tools:context=".MainActivity"
android:orientation="vertical" >

<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Messi"
android:onClick="onCheckboxClicked" />

<CheckBox
android:id="@+id/checkBox2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Ronaldo"
android:onClick="onCheckboxClicked" />
<Button
android:id="@+id/btnDisplay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Display"
android:onClick="onDisplayClicked" />
</LinearLayout>

3. Code 

Attach the onCheckBoxClick listner to both the checkboxs and when you click the checkbox it shows the checkbox text as an toast message.
Within the Activity that hosts this layout, the following method handles the click event for both checkboxes:


  1. If checkbox id : “checkBox1” is checked, display a floating box and display the checkbox state of first checkbox.
  2. If button is is clicked, display a floating box and display the  both checkbox states.

package com.learnsimply.checkbox;

import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Toast;

public class MainActivity extends Activity {

CheckBox checkBox1, checkBox2;
Button btnDisplay;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
checkBox1 = (CheckBox) findViewById(R.id.checkBox1);
checkBox2 = (CheckBox) findViewById(R.id.checkBox2);
}

public void onCheckboxClicked(View view) {
// Is the view now checked?
boolean checked = ((CheckBox) view).isChecked();
// Check which checkbox was clicked
switch(view.getId()) {
case R.id.checkBox1:
Toast.makeText(MainActivity.this,"Messi "+String.valueOf(checked),
Toast.LENGTH_SHORT).show();
break;
case R.id.checkBox2:
Toast.makeText(MainActivity.this,"Ronaldo "+String.valueOf(checked),
Toast.LENGTH_SHORT).show();
break;
}
}
public void onDisplayClicked(View view) {
StringBuffer result = new StringBuffer();
result.append("Messi check : ").append(checkBox1.isChecked());
result.append("\nRonaldo check : ").append(checkBox2.isChecked());
Toast.makeText(MainActivity.this, result.toString(),
Toast.LENGTH_LONG).show();
}

}

4. Run the application






Download Source Code checkboxExample.zip


1 comment: