THE WORLD'S LARGEST WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

CheckBox Tutorial Android


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


checked box in XML
<CheckBox
android:id="@+id/demoCheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="demo CheckBox"/>

You can add main file in java class isChecked() method work on Boolean xml code

//initiate a check box
CheckBox simpleCheckBox = (CheckBox) findViewById(R.id.demoCheckBox);

//check current state of a check box (true or false)
Boolean demoBoxState = demoCheckBox.isChecked();

Attributes CheckBox

idid is an attribute used to unique identify a check box
xml code of id
<CheckBox
android:id="@+id/demoCheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="demo CheckBox"/>
checked Checked is an attribute of check box used to set the current state of a check box. <CheckBox android:id="@+id/demoCheckBox" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="demo CheckBox" android:checked="true"/>
Set CheckBox In Java Class:
/*Add in Oncreate() funtion after setContentView()*/
// initiate a check box
CheckBox demoCheckBox = (CheckBox) findViewById(R.id.demoCheckBox);

// set the current state of a check box
simpleCheckBox.setChecked(true);
gravity Gravity attribute is an optional attribute which is used to control the alignment of the text in CheckBox bottom,left, right, center, top, center_vertical, center_horizontal.
 <CheckBox
    android:id="@+id/demoCheckBox"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="demo CheckBox"
    android:checked="true"
    android:gravity="right|center_vertical"/>  

texttext attribute is used to set the text in a check box.
<CheckBox
    android:id="@+id/demoCheckBox"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:checked="true"
    android:text="Text Attribute Of Check Box"/> 

Set text in CheckBox In Java class:
/*Add in Oncreate() funtion after setContentView()*/
// initiate check box
CheckBox demoCheckBox = (CheckBox) findViewById(R.id.demoCheckBox);

// displayed text of the check box
demoCheckBox.setText("Text Attribute Of Check Box");

textSize Textsize attribute used to set the size of text of a check box.text size must be sp and dp
<CheckBox
    android:id="@+id/demoCheckBox"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Text size Attribute Of Check Box"
    android:textColor="#0ff"
    android:checked="true"
    android:textSize="18sp"/>

set up in java class
/*Add in Oncreate() funtion after setContentView()*/
CheckBox demoCheckBox = (CheckBox) findViewById(R.id.demoCheckBox);
//set 18sp displayed text size
simpleCheckBox.setTextSize(18);

textColor textColor attribute is used to set the text color of a check box.
<CheckBox
    android:id="@+id/simpleCheckBox"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Text is blue Color"
    android:textColor="##00F" 
    android:checked="true"/> 


set up in java class

/*Add in Oncreate() funtion after setContentView()*/
//initiate the checkbox
CheckBox demoCheckBox = (CheckBox) findViewById(R.id.demoCheckBox);

//blue color for displayed text
demoCheckBox.setTextColor(Color.BLUE);
textSize textSize attribute is used to set the size of text of a check box blow the exmple of xml

<CheckBox
    android:id="@+id/demoCheckBox"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Text size Attribute Of Check Box"
    android:textColor="#0ff"
    android:checked="false"
    android:textSize="25dp"/>
set up in java class
/*Add in Oncreate() funtion after setContentView()*/
CheckBox demoCheckBox = (CheckBox) findViewById(R.id.demoCheckBox);
//set 25dp displayed text size
simpleCheckBox.setTextSize(25);
background this attribute is used to set the background of a check box.
<CheckBox
    android:id="@+id/simpleCheckBox"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Text size Attribute Of Check Box"
    android:textColor="#000"
    android:textSize="25sp"
    android:textStyle="bold|italic"
    android:checked="true"
    android:background="#00f" />
set up in java class

/*Add in Oncreate() funtion after setContentView()*/
CheckBox demoCheckBox = (CheckBox) findViewById(R.id.demoCheckBox);
// set background in CheckBox
simpleCheckBox.setBackgroundColor(Color.BLUE);
padding padding attribute is used to set the padding from left, right, top or bottom.
<CheckBox
    android:id="@+id/simpleCheckBox"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Padding Attribute Of Check Box"
    android:textColor="#44f" 
    android:textSize="20sp"
    android:textStyle="bold|italic"
    android:checked="false"
    android:padding="30dp"/> 

Example of CheckBox In Android

open android studio and create new project

Select File -> New -> New Project and Fill the forms and click "Finish" button.

Open res -> layout -> activity_main.xml (or) main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">

  <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Select Your search engine : "
        android:textColor="#f00"
        android:textSize="28sp"
        android:textStyle="bold" />

   <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="35dp"
        android:background="#e0e0e0"
        android:orientation="vertical">

      <CheckBox
            android:id="@+id/BaiduCheckBox"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:checked="false"
            android:padding="20dp"
            android:text="@string/android"
            android:textColor="#44f"
            android:textSize="20sp"
            android:textStyle="bold|italic" />

    <CheckBox
            android:id="@+id/GigablastCheckBox"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:checked="false"
            android:padding="20dp"
            android:text="@string/java"
            android:textColor="#f44"
            android:textSize="20sp"
            android:textStyle="bold|italic" />
<CheckBox
            android:id="@+id/GoogleCheckBox"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:checked="false"
            android:padding="20dp"
            android:text="@string/php"
            android:textColor="#444"
            android:textSize="20sp"
            android:textStyle="bold|italic" />

<CheckBox
            android:id="@+id/YahooCheckBox"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:checked="false"
            android:padding="20dp"
            android:text="@string/python"
            android:textColor="#888"
            android:textSize="20sp"
            android:textStyle="bold|italic" />
<CheckBox
            android:id="@+id/YandexCheckBox"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:checked="false"
            android:padding="25dp"
            android:text="@string/unity"
            android:textColor="#fff"
            android:textSize="18sp"
            android:textStyle="bold|italic" />
    </LinearLayout>

</RelativeLayout>

Open app -> java-> package -> MainActivity.java
package example.welookups.checkboxexample;

import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    CheckBox Baidu, Gigablast, Google, Yahoo, Yandex;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // initiate views
        android = (CheckBox) findViewById(R.id.BaiduCheckBox);
        android.setOnClickListener(this);
        java = (CheckBox) findViewById(R.id.GigablastCheckBox);
        java.setOnClickListener(this);
        python = (CheckBox) findViewById(R.id.GoogleCheckBox);
        python.setOnClickListener(this);
        php = (CheckBox) findViewById(R.id.YahooCheckBox);
        php.setOnClickListener(this);
        unity3D = (CheckBox) findViewById(R.id.YandexCheckBox);
        unity3D.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {

        switch (view.getId()) {
            case R.id.BaiduCheckBox:
                if (android.isChecked())
                    Toast.makeText(getApplicationContext(), "Baidu", Toast.LENGTH_LONG).show();
                break;
            case R.id.javaCheckBox:
                if (Gigablast.isChecked())
                    Toast.makeText(getApplicationContext(), "Gigablast", Toast.LENGTH_LONG).show();
                break;
            case R.id.phpCheckBox:
                if (Google.isChecked())
                    Toast.makeText(getApplicationContext(), "Google", Toast.LENGTH_LONG).show();
                break;
            case R.id.pythonCheckBox:
                if (Yahoo.isChecked())
                    Toast.makeText(getApplicationContext(), "Yahoo", Toast.LENGTH_LONG).show();
                break;
            case R.id.unityCheckBox:
                if (Yandex.isChecked())
                    Toast.makeText(getApplicationContext(), "Yandex", Toast.LENGTH_LONG).show();
                break;
        }
    }
}
open res ->values -> strings.xml
<resources>
    CheckBoxdemo
    Hello world!
    Settings
    Baidu
    Gigablast
    Google
    Yahoo
    Yandex