Monday, June 18, 2012

Example of Checkbox in android application with Eclipse

File : BeanMatrixDemo/res/values/String.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, BeanMatrixDemoActivity!</string>
<string name="app_name">BeanMatrixDemo</string>
<string name="btn_display">Display</string>
<string name="chk_java">java</string>
<string name="chk_Asp">ASP.NET</string>
<string name="chk_cpp">C++</string>
</resources>
File : BeanMatrixDemo/res/layout/main.xml

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


<CheckBox
    android:id="@+id/chkJava"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/chk_java" />

<CheckBox
        android:id="@+id/chkAsp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/chk_Asp"
        android:checked="true" />

<CheckBox
        android:id="@+id/chkCpp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/chk_cpp" />

<Button
        android:id="@+id/btnDisplay"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/btn_display" />

</LinearLayout>


File:  BeanMatrixDemo/src/com/beanmatrix/demo/beanmatrixactivity.java
package com.beanmatrix.demo;

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 BeanMatrixCheckbox extends Activity{
       private CheckBox chkJava, chkAsp, chkCpp;
         private Button btnDisplay;

         @Override
         public void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.checkbox);
              addListenerOnchkJava();
              addListenerOnButton();
         }

         public void addListenerOnchkJava() {
              chkJava = (CheckBox) findViewById(R.id.chkJava);
              chkJava.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {
                       //is chkJava checked?
                       if (((CheckBox) v).isChecked()) {
                           Toast.makeText(BeanMatrixCheckbox.this,
                               "Bro, try Android :)", Toast.LENGTH_LONG).show();
                     }
                }
              });
         }

         public void addListenerOnButton() {
              chkJava = (CheckBox) findViewById(R.id.chkJava);
                          chkAsp = (CheckBox) findViewById(R.id.chkAsp);
                          chkCpp = (CheckBox) findViewById(R.id.chkCpp);
              btnDisplay = (Button) findViewById(R.id.btnDisplay);
              btnDisplay.setOnClickListener(new OnClickListener() {
                 //Run when button is clicked
                public void onClick(View v) {
                     StringBuffer result = new StringBuffer();
                     result.append("IPhone check : ").append(chkJava.isChecked());
                     result.append("\nAndroid check : ").append(chkAsp.isChecked());
                     result.append("\nWindows Mobile check :").append(chkCpp.isChecke());
                     Toast.makeText(BeanMatrixCheckbox.this, result.toString(),
                                  Toast.LENGTH_LONG).show();
                }

              });
          }
       }


 




1 comment: