Tuesday, April 10, 2012

check box in android


CheckBoxTestActivity.java
------------------------------------
package home.checkBoxTest;

import android.app.Activity;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.Toast;

public class CheckBoxTestActivity extends Activity
{
CheckBox chkBoxAndroid;
CheckBox chkBoxIPhone;
CheckBox chkBoxBlackBerry;

@Override
protected void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

initialUISetup();

chkBoxAndroid.setOnCheckedChangeListener(new myCheckBoxChnageClicker());
chkBoxBlackBerry.setOnCheckedChangeListener(new myCheckBoxChnageClicker());
chkBoxIPhone.setOnCheckedChangeListener(new myCheckBoxChnageClicker());

}
public void initialUISetup()
{
    chkBoxAndroid = (CheckBox) findViewById(R.id.chkBoxAndroid);
    chkBoxIPhone = (CheckBox) findViewById(R.id.chkBoxIPhone);
    chkBoxBlackBerry = (CheckBox) findViewById(R.id.chkBoxBlackBerry);    
}

class myCheckBoxChnageClicker implements CheckBox.OnCheckedChangeListener
    {

           public void onCheckedChanged(CompoundButton buttonView,boolean isChecked)
           {
                 if(isChecked)
                 {
                      if(buttonView==chkBoxAndroid)
                       {
                           showTextNotification("Android");
                       }
           
                       if(buttonView==chkBoxIPhone)
                       {
                           showTextNotification("iPhone");
                       }
           
                       if(buttonView==chkBoxBlackBerry)
                       {
                           showTextNotification("BlackBerry");
                       }
                 }
           } //onCheckedChanged
    } //myCheckBoxChnageClicker

public void showTextNotification(String msgToDisplay)
    {
           Toast.makeText(getApplicationContext(), msgToDisplay, Toast.LENGTH_SHORT).show();
    }
} //CheckBoxTestActivity class


main.xml
--------------------------------

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <CheckBox
    android:text="Android"
    android:id="@+id/chkBoxAndroid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true">
</CheckBox>

<CheckBox
android:text="iPhone"
android:id="@+id/chkBoxIPhone"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</CheckBox>

<CheckBox
android:text="BlackBerry"
android:id="@+id/chkBoxBlackBerry"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</CheckBox>
</LinearLayout>



No comments:

Post a Comment