What is checkbox

Android Checkbox is a GUI widgets that permits the user to make choice of two state checked or unchecked.

Checkbox provide the facility of multiple choice i.e your known languages like c , c++ , java ,c# , php where you can use the checkbox for making choice in android application.

The general use of checkbox you see in the form that you fill for job where you find the option like known language hindi , english and where you need the tick the language in form.


CheckBox:.

In this section i am going to show the CheckBox with practical example of known languges like c , c++ java, C# .

First open the android studio and create a project and give the name of your project like CheckboxDemo.

The screenshots of the program is as follows:

Smiley face Smiley face

Drag and drop the four CheckBox and one Button and one TextView. Open the activity_main.xml file and code look like this


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.niitpc.myapplication.MainActivity">


 <CheckBox
        android:id="@+id/checkBox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="67dp"
        android:layout_marginStart="67dp"
        android:layout_marginTop="57dp"
        android:text="C" />

 <CheckBox
        android:id="@+id/checkBox2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/checkBox"
        android:layout_alignStart="@+id/checkBox"
        android:layout_below="@+id/checkBox"
        android:layout_marginTop="43dp"
        android:text="C++" />

 <CheckBox
        android:id="@+id/checkBox3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/checkBox2"
        android:layout_alignStart="@+id/checkBox2"
        android:layout_below="@+id/checkBox2"
        android:layout_marginTop="43dp"
        android:text="Java" />

 <CheckBox
        android:id="@+id/checkBox4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/checkBox3"
        android:layout_alignStart="@+id/checkBox3"
        android:layout_below="@+id/checkBox3"
        android:layout_marginTop="45dp"
        android:text="C#" />

  <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/checkBox4"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="37dp"
        android:onClick="display"
        android:text="Button" />

  <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button"
        android:layout_alignStart="@+id/button"
        android:layout_alignTop="@+id/checkBox3"
        android:layout_marginLeft="74dp"
        android:layout_marginStart="74dp"
        android:text="Result" />


</RelativeLayout>

       

Open the MainActivity.java file and write code look like this.

Here in the program you check the CheckBoxes and click on the Button it wil dislay the selected langauge in TextView.


      package com.example.niitpc.myapplication;

import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.MediaController;
import android.widget.TextView;
import android.widget.Toast;//you must add this line other wise it gettting error.
import android.widget.VideoView;

public class MainActivity extends AppCompatActivity {


    CheckBox checkBox1;//for C langauge
    CheckBox checkBox2;//for C++ language
    CheckBox checkBox3;//for Java language
    CheckBox checkBox4;//for C#
    TextView textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        checkBox1=(CheckBox) findViewById(R.id.checkBox);
        checkBox2=(CheckBox) findViewById(R.id.checkBox2);
        checkBox3=(CheckBox) findViewById(R.id.checkBox3);
        checkBox4=(CheckBox) findViewById(R.id.checkBox4);
        textView=(TextView)findViewById(R.id.textView);

    }
    public void display(View v)
    {
        StringBuffer buffer =new StringBuffer();
        buffer.append("Selected Language:");
        if(checkBox1.isChecked())
        {
            buffer.append("\nC");

        }
        if(checkBox2.isChecked())
        {
            buffer.append("\nC++");

        }
        if(checkBox3.isChecked())
        {
            buffer.append("\nJava");

        }
        if(checkBox4.isChecked())
        {
            buffer.append("\nC#");

        }

        textView.setText(buffer.toString());


    }

}

       

Explanation:

CheckBox checkbox1:This statement create the object checkbox1 of CheckBox .Here i am going to use checkbox1 object for C language.

CheckBox checkbox2:This statement create the object checkbox2 of CheckBox .Here i am going to use checkbox2 object for C++ language.

CheckBox checkbox3:This statement create the object checkbox3 of CheckBox .Here i am going to use checkbox3 object for Java language.

CheckBox checkbox4:This statement create the object checkbox4 of CheckBox .Here i am going to use checkbox4 object for C# language.

TextView textView:This statement create the object textView of TextView .Here i am going to use textView object for display the result.

Button button:This statement create the object button of Button .Here i am going to use button object for Clicking the result.

checkBox1=(CheckBox)findViewById(R.id.checkBox);This statement initialized the checkbox1 object.

checkBox2=(CheckBox)findViewById(R.id.checkBox2);This statement initialized the checkbox2 object.

checkBox3=(CheckBox)findViewById(R.id.checkBox3);This statement initialized the checkbox3 object.

checkBox4=(CheckBox)findViewById(R.id.checkBox4);This statement initialized the checkbox4 object.

textView=(TextView)findViewById(R.id.textView);This statement initialized the textView object.

After initialization i have to created the display()

StringBuffer buffer =new StringBuffer();This statement create the object buffer of StringBuffer class for storing the language like C,C++,Java,C# after check the CheckBox.

buffer.append("Selected Language:");This statement append the Selected Language in buffer object.

if(checkBox1.isChecked()) { buffer.append("\nC"); }This statement check the CheckBox is checked or not with the help of isChecked().If CheckBox is checked than the buffer append the C in next line using \n

if(checkBox2.isChecked()) { buffer.append("\nC++"); }This statement check the CheckBox is checked or not with the help of isChecked().If CheckBox is checked than the buffer append the C++ in next line using \n

if(checkBox3.isChecked()) { buffer.append("\nJava"); }This statement check the CheckBox is checked or not with the help of isChecked().If CheckBox is checked than the buffer append the Java in next line using \n

if(checkBox4.isChecked()) { buffer.append("\nC#"); }This statement check the CheckBox is checked or not with the help of isChecked().If CheckBox is checked than the buffer append the C# in next line using \n

textView.setText(buffer.toString());This statement set the buffer value into textView object using setText().

You do not forget to set the android:onClick="display".If you do not set this property button will not perform any action .


checkbox

What is checkbox for video here

Smiley face                     Smiley face