Wednesday 26 December 2012

Android Checkbox Tutorial

Android Checkbox Tutorial

In this tutorial am going to show how to use checkbox in android.Basically the checkbox is used for receive user input.Lets find how it works in android.

STEP BY STEP

1.Create an Android Project


2.Create a strings.xml file in res/values folder.
    Copy and paste the given bellow code.

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">CheckboxExample</string>
    <string name="linux_box">Android</string>
    <string name="macos_box">Apple</string>
    <string name="windows_box">Windows</string>
    <string name="display_label">Display</string>
    <string name="menu_settings">Settings</string>
</resources>



3.In this project we are adding three checkbox and when any of the checkbox   
   is selected,the corresponding message will be shown in a toast.

4.Open the main.xml file and copy and paste the bellow code.


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

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Android iTuts"
        android:paddingLeft="130px"
        android:textAppearance="?  android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Your choice"
        android:textAppearance="?android:attr/textAppearanceMedium" />

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

    <CheckBox
        android:id="@+id/checkapple"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Apple" />

    <CheckBox
        android:id="@+id/checkwindows"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Windows" />


    
</LinearLayout>


5.Open the main Activity file and put the given bellow code.


package com.roney.checkbox;

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

public class CheckboxActivity extends Activity 
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) 
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
       
CheckBox android=(CheckBox) findViewById(R.id.checkandroid);
android.setOnClickListener(new OnClickListener() 
{
@Override
public void onClick(View v) 
{
if (((CheckBox) v).isChecked()) 
{
Toast.makeText(CheckboxActivity.this,"Your choice is Android", Toast.LENGTH_LONG).show();
}
}
});
        
CheckBox apple=(CheckBox) findViewById(R.id.checkapple);
        
apple.setOnClickListener(new OnClickListener() 
{
@Override
public void onClick(View v) 
{
if (((CheckBox) v).isChecked()) 
{
Toast.makeText(CheckboxActivity.this,"Your choice is Apple", Toast.LENGTH_LONG).show();
}
}
});
CheckBox windows=(CheckBox) findViewById(R.id.checkwindows);
        
windows.setOnClickListener(new OnClickListener() 
{

@Override
public void onClick(View v) 
{
if (((CheckBox) v).isChecked()) 
{
Toast.makeText(CheckboxActivity.this,"Your choice is Windows", Toast.LENGTH_LONG).show();
}
}
});
}
}


6.Ouput is shown bellow





If you need to run this application in the android smartphone


1 comments:

Anonymous said...

Is it possible to add actionPerformed event in checkbox??