Android CheckBox Example - ANDROID - Helper

Thursday, February 24, 2011

Android CheckBox Example


CHECKBOX


Android checkbox is used to select more than one option at a time.
Example:  In an admission form we may need to select both diploma & BE in Graduate option.
For this we can use checkbox which will enable us to select multiple options.



SOURCE CODE [main.xml] is

<xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/LinearLayout01"
                android:layout_width="fill_parent"

android:layout_height="fill_parent"
                xmlns:android="http://schemas.android.com/apk/res/android"
                android:orientation="vertical">
                <CheckBox android:id="@+id/check1"
android:layout_width="100px"
                                android:layout_height="wrap_content"
android:text="Android" />
                <CheckBox android:id="@+id/check2"
android:layout_width="100px"
                                android:layout_height="wrap_content"
android:text="iPhone" />
                <Button android:id="@+id/button1"
android:layout_width="100px"
                                android:layout_height="wrap_content"
android:text="Check" />
</LinearLayout>
 
SOURCE CODE [CheckBoxExample.java] is

package com.CheckBoxExample;

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 CheckBoxExample extends Activity implements OnClickListener
{
Button b1;
CheckBox c1, c2;
Toast msg;

public void onCreate(Bundle icicle)
{
                super.onCreate(icicle);
                setContentView(R.layout.main);

                c1 = (CheckBox) findViewById(R.id.check1);
                c2 = (CheckBox) findViewById(R.id.check2);
                b1 = (Button) findViewById(R.id.button1);
                b1.setOnClickListener(this);
}

public void onClick(View arg0)
{
                // TODO Auto-generated method stub
                if (c1.isChecked()==true && c2.isChecked()==true)
                {
                                msg = Toast.makeText(CheckBoxExample.this,
                                                "Android and Iphone", Toast.LENGTH_SHORT);
                                 msg.show();
                }
                if (c1.isChecked()==true && c2.isChecked()==false)
                                {
                                msg = Toast.makeText(CheckBoxExample.this,
                                                "Android", Toast.LENGTH_SHORT);
                                                 msg.show();
                                }
                                if (c1.isChecked()==false && c2.isChecked()==true)
                                {
                                                msg = Toast.makeText(CheckBoxExample.this,
                                                                "Iphone", Toast.LENGTH_SHORT);
                                                msg.show();
                                }
                                if (c1.isChecked()==false && c2.isChecked()==false)
{
                                                msg = Toast.makeText(CheckBoxExample.this,
                                                                "Nothing Selected", Toast.LENGTH_SHORT);
                                                msg.show();
                }
                }

}


The OUTPUT will be

https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEigZt_LIYFgm3V0nGdoyKh7Kdmm3107zbueW5U-lRbXRYA8nuxj0QbFG63fuZ1Af4wXEkCLt-e4Zuhx-YGkAWSc6lGtAO3fDipimOKLo5ExxVa_cqjRS2xohYhu_LR8NSLfvFyhYY_rWDI/ https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEig1Qt1JUDoF9zTvBu6HLVS0_PXrjxTdpgTi7HuNcYAQFcG6HWK6Si43XuExaF180uKp75T-KAFuOgJbrggaSfQ1nk2N5nhO9MtuJxegRneMyX-BjRKo5w98z9vncL307odD5Ke8UmZ2ik/
 

4 comments:

  1. It was very helpful for me.Thank you very much.

    ReplyDelete
  2. I very like to learn at this site.site is very wonderful and useful.

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete