Cheap VPS & Xen Server


Residential Proxy Network - Hourly & Monthly Packages

CheckBox


Android CheckBox is a type of two state button either checked or unchecked.

There can be a lot of usage of checkboxes. For example, it can be used to know the hobby of the user, activate/deactivate the specific action etc.

Android CheckBox class is the subclass of CompoundButton class.

Android CheckBox class

The android.widget.CheckBox class provides the facility of creating the CheckBoxes.

Methods of CheckBox class

There are many inherited methods of View, TextView, and Button classes in the CheckBox class. Some of them are as follows:

Method Description
public boolean isChecked() Returns true if it is checked otherwise false.
public void setChecked(boolean status) Changes the state of the CheckBox.

Android CheckBox Example

activity_main.xml

Drag the three checkboxes and one button for the layout. Now the activity_main.xml file will look like this:

File: activity_main.xml
  1. <RelativeLayout xmlns:android=“http://schemas.android.com/apk/res/android”
  2.     xmlns:tools=“http://schemas.android.com/tools”
  3.     android:layout_width=“match_parent”
  4.     android:layout_height=“match_parent”
  5.     tools:context=“.MainActivity” >
  6.     <CheckBox
  7.         android:id=“@+id/checkBox1”
  8.         android:layout_width=“wrap_content”
  9.         android:layout_height=“wrap_content”
  10.         android:layout_alignParentLeft=“true”
  11.         android:layout_alignParentTop=“true”
  12.         android:text=“Pizza” />
  13.     <CheckBox
  14.         android:id=“@+id/checkBox2”
  15.         android:layout_width=“wrap_content”
  16.         android:layout_height=“wrap_content”
  17.         android:layout_alignParentTop=“true”
  18.         android:layout_toRightOf=“@+id/checkBox1”
  19.         android:text=“Coffe” />
  20.     <CheckBox
  21.         android:id=“@+id/checkBox3”
  22.         android:layout_width=“wrap_content”
  23.         android:layout_height=“wrap_content”
  24.         android:layout_alignParentTop=“true”
  25.         android:layout_toRightOf=“@+id/checkBox2”
  26.         android:text=“Burger” />
  27.     <Button
  28.         android:id=“@+id/button1”
  29.         android:layout_width=“wrap_content”
  30.         android:layout_height=“wrap_content”
  31.         android:layout_below=“@+id/checkBox2”
  32.         android:layout_marginTop=“32dp”
  33.         android:layout_toLeftOf=“@+id/checkBox3”
  34.         android:text=“Order” />
  35. </RelativeLayout>

Activity class

Let’s write the code to check which toggle button is ON/OFF.

File: MainActivity.java
  1. package com.example.checkbox;
  2. import android.os.Bundle;
  3. import android.app.Activity;
  4. import android.view.Menu;
  5. import android.view.View;
  6. import android.view.View.OnClickListener;
  7. import android.widget.*;
  8. public class MainActivity extends Activity {
  9.     CheckBox pizza,coffe,burger;
  10.     Button buttonOrder;
  11.     @Override
  12.     protected void onCreate(Bundle savedInstanceState) {
  13.         super.onCreate(savedInstanceState);
  14.         setContentView(R.layout.activity_main);
  15.         addListenerOnButtonClick();
  16.     }
  17. public void addListenerOnButtonClick(){
  18.     //Getting instance of CheckBoxes and Button from the activty_main.xml file
  19.     pizza=(CheckBox)findViewById(R.id.checkBox1);
  20.     coffe=(CheckBox)findViewById(R.id.checkBox2);
  21.     burger=(CheckBox)findViewById(R.id.checkBox3);
  22.     buttonOrder=(Button)findViewById(R.id.button1);
  23.     //Applying the Listener on the Button click
  24.     buttonOrder.setOnClickListener(new OnClickListener(){
  25.         @Override
  26.         public void onClick(View view) {
  27.             int totalamount=0;
  28.             StringBuilder result=new StringBuilder();
  29.             result.append(“Selected Items:”);
  30.             if(pizza.isChecked()){
  31.                 result.append(“\nPizza 100Rs”);
  32.                 totalamount+=100;
  33.             }
  34.             if(coffe.isChecked()){
  35.                 result.append(“\nCoffe 50Rs”);
  36.                 totalamount+=50;
  37.             }
  38.             if(burger.isChecked()){
  39.                 result.append(“\nBurger 120Rs”);
  40.                 totalamount+=120;
  41.             }
  42.             result.append(“\nTotal: “+totalamount+“Rs”);
  43.             //Displaying the message on the toast
  44.             Toast.makeText(getApplicationContext(), result.toString(), Toast.LENGTH_LONG).show();
  45.         }
  46.     });
  47. }
  48.     @Override
  49.     public boolean onCreateOptionsMenu(Menu menu) {
  50.         // Inflate the menu; this adds items to the action bar if it is present.
  51.         getMenuInflater().inflate(R.menu.activity_main, menu);
  52.         return true;
  53.     }
  54. }

Output:

checkboxoutput1

Comments

comments