Monday, 4 June 2012

Simple Check Box Demo


/* This is First Basic Demo of Check Box Android */

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" >

    <CheckBox android:id="@+id/cb1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Male" />
 
<CheckBox android:id="@+id/cb2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Female" />
   
</LinearLayout>


CheckboxDemoActivity.java


package com.checkbox;

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

public class CheckboxDemoActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
     
        final CheckBox check_box1 = (CheckBox) findViewById(R.id.cb1);
        final CheckBox check_box2 = (CheckBox) findViewById(R.id.cb2);
        check_box1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
        if(check_box1.isChecked())
        {
        Toast.makeText(CheckboxDemoActivity.this, "I am a male",Toast.LENGTH_SHORT).show();
        }
        }
        });
        check_box2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            if(check_box2.isChecked())
            {
                Toast.makeText(CheckboxDemoActivity.this, "I am a female",Toast.LENGTH_SHORT).show();
            }
            }
            });
    }
}





0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | Best Web Host