Android RadioButton

In this tutorial, it shows you on how to create 3 checkboxes in XML file, and demonstrates the use of listener to check or uncheck the checkbox
How it works?
1. Open “res/values/strings.xml” file, add some user-defined string.

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="chkbsit">BSIT</string>
    <string name="chkbscoe">BSCoE</string>
    <string name="chkitp">ITP</string>
    <string name="btndisplay">Display</string>
    <string name="app_name">ESS CheckBox</string>
</resources>
2. Open “res/layout/main.xml” file, add 3 “CheckBox” and a button, inside the LinearLayout.
<?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" >
    <CheckBox
        android:id="@+id/chkBSIT"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/chkbsit" />
      <CheckBox
          android:id="@+id/chkBSCOE"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/chkbscoe" />
      <CheckBox
          android:id="@+id/chkITP"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/chkitp" />
       <Button
          android:id="@+id/btnDISPLAY"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/btndisplay" />
</LinearLayout>
3. Attach listeners inside your activity “onCreate()” method, to monitor following events :
  • If checkbox id : “chkbsit” is checked, display a floating box with message “Go Webhunters”.
  • If button is clicked, display a floating box and display the checkbox states.
package ess.checkbox.isourcecode;
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 ESSCheckBoxActivity extends Activity implements OnClickListener {
    /** Called when the activity is first created. */
    CheckBox cbsitcbscoecitp;
    Button bdisplay;
      @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        cbsit=(CheckBox)findViewById(R.id.chkBSIT);
        cbscoe=(CheckBox)findViewById(R.id.chkBSCOE);
        citp=(CheckBox)findViewById(R.id.chkITP);
        bdisplay=(Button)findViewById(R.id.btnDISPLAY);
        bdisplay.setOnClickListener(this);
        cbsit.setOnClickListener(this);
    }
      public void onClick(View v) {
            // TODO Auto-generated method stub
        if(v.getId()==R.id.btnDISPLAY){
           StringBuffer result = new StringBuffer();
           result.append("BSIT check:").append(cbsit.isChecked() + "\n");
           result.append("BSCOE check:").append(cbscoe.isChecked() + "\n");
           result.append("ITP check:").append(citp.isChecked());
           Toast.makeText(this, result.toString(), Toast.LENGTH_LONG).show();
       }else if(v.getId()==R.id.chkBSIT){
           if (cbsit.isChecked()){
              Toast.makeText(this"Go WebHunters!", Toast.LENGTH_LONG).show();
                  }
            }
      }
}

4. Run the application.

Programming Language: Java + XML
Programming IDE: Eclipse