Thursday, July 31, 2014

Use Checkbox in Android

Form controls play an important role in accepting data from the user. We shall see examples of the various form controls one by one. Let's start with the "checkbox".
In Android, we can use “android.widget.CheckBox” class to render a checkbox. In this tutorial, we shall learn how to create 3 checkboxes in XML file, see the use of listener to read the checkbox state – checked or unchecked, and the way to keep a checkbox checked by default.

1. Strings.xml file

Open the strings.xml file and add some user-defined strings to display text on the screen.
File : res/values/strings.xml


<resources>
    <string name="app_name">MyApp</string>
<string name="chk_ios">IPhone</string>
<string name="chk_android">Android</string>
<string name="chk_bb">Blackberry</string>
<string name="btn_display">Display</string>
</resources>

2. Add the checkbox field

Open the main.xml file and add 3 checkboxes and a button (which would show the checkbox state when pressed).
File : res/layout/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:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Which OS do you use?"
        android:textSize="25sp" />
   
<CheckBox android:id="@+id/chkAndroid"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/chk_android"
        android:textSize="25sp"
        android:checked="true" />
   
<CheckBox android:id="@+id/chkBB"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="25sp"
        android:text="@string/chk_bb" />
      
<CheckBox android:id="@+id/chkIos"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="25sp"
        android:text="@string/chk_ios" />
  
<Button android:id="@+id/btnDisplay"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="25sp"
        android:text="@string/btn_display" />
</LinearLayout>
Note: To keep a checkbox checked by default, set the android:checked attribute to "true".

3. Java code

We shall now attach listeners inside the activity “onCreate()” method, to monitor the following events :

i. If checkbox id : "chkBB"or “chkIos”  is checked, display a toast message “Try Android Buddy ;)”
ii. If the "Display" button is is clicked, display a toast message and show the checkbox states.
File: HomeScreen.java

package com.endeavour.android;
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.Toast;
public class HomeScreen extends Activity {
private CheckBox chkIos, chkAndroid, chkBB;
private Button btnDisplay;
@Override
public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.main);
       addListenerOnChkBB();
       addListenerOnChkIos();
       addListenerOnButton();
}
public void addListenerOnChkIos() {
       chkIos = (CheckBox) findViewById(R.id.chkIos);
       chkIos.setOnClickListener(new OnClickListener() {
         @Override
         public void onClick(View v) {
                //is chkIos checked?
              if (((CheckBox)v).isChecked()) {
                     Toast.makeText(HomeScreen.this,"Try Android Buddy ;)", Toast.LENGTH_LONG).show();
              }
         }
       });
}
public void addListenerOnChkBB() {
       chkBB = (CheckBox) findViewById(R.id.chkBB);
       chkBB.setOnClickListener(new OnClickListener() {
         @Override
         public void onClick(View v) {
                //is chkBB checked?
              if (((CheckBox)v).isChecked()) {
                     Toast.makeText(HomeScreen.this,"Try Android Buddy ;)", Toast.LENGTH_LONG).show();
             }
         }
       });
}
public void addListenerOnButton() {
       chkIos = (CheckBox) findViewById(R.id.chkIos);
       chkAndroid = (CheckBox) findViewById(R.id.chkAndroid);
       chkBB = (CheckBox) findViewById(R.id.chkBB);
       btnDisplay = (Button) findViewById(R.id.btnDisplay);
       btnDisplay.setOnClickListener(new OnClickListener() {
          //Run when button is clicked
         @Override
         public void onClick(View v) {
         StringBuffer result = new StringBuffer();
         result.append("Android check: ").append(chkAndroid.isChecked());
         result.append("\nBlackberry check :").append(chkBB.isChecked());
         result.append("\nIPhone check : ").append(chkIos.isChecked());
         Toast.makeText(HomeScreen.this, result.toString(),Toast.LENGTH_LONG).show();
         }
       });
}
}

4. Output

The following output shall be displayed:

i. When the checkbox for Blackberry is checked


ii. When the Display button is clicked


No comments:

Post a Comment