16 December 2014

Code for a Check-Box in Android

Here we will learn to make a check-box and various actions in check-box with an example and total explanation of the code .

What we will need ?
  • A layout(activity_main.xml)
  • A Java Source (MainActivity.java)
  • 3 Check-boxes
  • Toast
Step 1 : Create a new Android Application by clicking on File->New-> Android Application Project.
file new

Step 2 : Give a suitable name to your Project and a package name also (CheckBoxDemo).
name checkbox
Click Next on other steps till you exit the small window (at last click Finish).

Step 3 : Design the xml layout for our Application (activity_main.xml) found in res->layout form the Project Explorer.
drag checkbox pallete
activity_main.xml 
<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:id="@+id/chickenCB"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:checked="true"
    android:text="Chicken" />

<CheckBox android:id="@+id/fishCB" 
    android:text="Fish"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

<CheckBox android:id="@+id/steakCB"
    android:text="Steak"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

</LinearLayout>


Step 4 :Design the code for java back-end in MainActivity.java found in src folder from Project Explorer.

MainActivity.java 
package com.mia.checkboxdemo;

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

public class MainActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final CheckBox fishCB = (CheckBox)findViewById(R.id.fishCB);
        final CheckBox chickenCB = (CheckBox)findViewById(R.id.chickenCB);
        final CheckBox steakCB = (CheckBox)findViewById(R.id.steakCB);
        
    fishCB.setOnCheckedChangeListener(
            new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton arg0, boolean isChecked) {
                    if(fishCB.isChecked()==false)
                        Toast.makeText(getApplicationContext(), "Fish not selected", Toast.LENGTH_SHORT).show();    
                    else
                        Toast.makeText(getApplicationContext(), "Fish Selected", Toast.LENGTH_SHORT).show();
                }});
    
chickenCB.setOnCheckedChangeListener(
        new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton arg0, boolean isChecked) {
                
                if(chickenCB.isChecked()==false)
                    Toast.makeText(getApplicationContext(), "Chicken not selected", Toast.LENGTH_SHORT).show();    
                else
                    Toast.makeText(getApplicationContext(), "Chicken Selected", Toast.LENGTH_SHORT).show();
                        
            }});
    
if(steakCB.isChecked())
    steakCB.toggle();  // flips the checkbox to unchecked if it was checked
steakCB.setOnCheckedChangeListener(
    new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton arg0, boolean isChecked) {
            
            if(steakCB.isChecked()==false)
                Toast.makeText(getApplicationContext(), "Steak not selected", Toast.LENGTH_SHORT).show();    
            else
                Toast.makeText(getApplicationContext(), "Steak Selected", Toast.LENGTH_SHORT).show();
        }});
    
    }
    }


Step 5 : Run the Project.
chicken fish checkchicken not check
Explanation of the code :
  1. Explanation from activity_main.xml
  • We drag three check-boxes from the Palette and add text for the display of Fish Chicken and Steak. We assign the ids fishCB, chickenCB and steakCB respectively.
  • In the check-box code of Fish the statement 
android:checked="true"
          says that it will be previously checked(ticked) before running the project.
  • Others by default will be unchecked (not ticked).
      2.  Explanation of MainActivity.java
  • We define the variables of type CheckBox for our check-boxes using method findViewById() an the ids present in R.java.
final CheckBox fishCB = (CheckBox)findViewById(R.id.fishCB);
        final CheckBox chickenCB = (CheckBox)findViewById(R.id.chickenCB);
        final CheckBox steakCB = (CheckBox)findViewById(R.id.steakCB);
fishCB.setOnCheckedChangeListener(
            new CompoundButton.OnCheckedChangeListener()
         It just maintains the state of the check-box(checked or unchecked).
  • Then a method is defined in it named onCheckedChanged() in which we define out actual code. We use an if-else loop.
public void onCheckedChanged(CompoundButton arg0, boolean isChecked)
  • In the if part we we make a Toast for the check-box being unchecked by checking if isChecked() method returns false value else the vice-versa.We repeat the same procedure for the remaining 2 check-boxes.
if(chickenCB.isChecked()==false)

Toast.makeText(getApplicationContext(), "Chicken not selected", Toast.LENGTH_SHORT).show();    

else

Toast.makeText(getApplicationContext(), "Chicken Selected", Toast.LENGTH_SHORT).show();

 }});

Github download

Stay Tuned with Made In Android

Previous Page Next Page Home
Top