Android CheckBox Control

0
440

A CheckBox control acts as an on and off switch which can be controlled by the user. The two states of this UI control are checked and unchecked. A user can either keep the offered feature either in the checked state or in the unchecked state. This CheckBox UI control can be used in a situation when a group of selectable options is offered to a user. Make sure that these selectable items are mutually exclusive from one another.

Also Read: Android ToggleButton Control

Various CheckBox Attributes
In the below provided tables, you will get a list of major attributes of CheckBox control. For the complete list of CheckBox attributes, take a look at the official Android document. In there, you will also find the associated methods that are capable of altering the values of the CheckBox attributes during the runtime of your application. 

The following list of CheckBox attributes is inherited from the android.widget.TextView class. 

S. No. Attribute Description
1 android:text * The text passed to this attribute will be displayed on the application’s screen.
2 android:autoText * This attribute accepts the textual input which can auto-correct the common spelling errors.
3 android:editable * The value of this attribute decides whether the TextView can have an input method or not.
4 android:drawableRight * The drawable value passed to this attribute is displayed to the right of the text.
5 android:drawableBottom * The drawable value passed to this attribute is displayed below the text.

 

The following list of CheckBox attributes is inherited from the android.view.View class. 

S. No. Attribute Description
1 android:id * The value of this attribute is accepted as the id of the view.
2 android:background * This drawable value gets accepted as the view’s background.
3 android:visibility * If set, the attribute controls the view’s initial visibility.
4 android:contentDescription * The view’s content is shortly described within this attribute.
5 android:onClick * The method gets invoked whenever a user clicks on the view.

 

The following example will help you to understand the creation of an Android application with the help of Linear layout and CheckBox control.

Step 1: Start with creating an application on the Android Studio IDE. Name this newly created Android application as myapplication. Make sure you keep this file under the com.example.myapplication package. For the better clarity of this step, you can take a look at the Hello World Example chapter. 

Step 2: Follow the src/ directory where you will find the MainActivity.java file. In this file, you need to add a click event. Other than your coding, the file will be containing the definitions of fundamental lifecycle callback methods. 

package com.example.myapplication;
import android.os.Bundle;
import android.app.Activity;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.Toast; 

public class MainActivity extends Activity
{
   CheckBox ch1,ch2;
   Button b1,b2; 

   @Override
   protected void onCreate(Bundle savedInstanceState)
   {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);      
      ch1=(CheckBox)findViewById(R.id.checkBox1);
      ch2=(CheckBox)findViewById(R.id.checkBox2);      
      b1=(Button)findVieById(R.id.button);
      b2=(Button)findViewById(R.id.button2);
      b2.setOnClickListener(new View.OnClickListener()
      {
         @Override
         public void onClick(View v)
         {
            finish();
         }
      });
      b1.setOnClickListener(new View.OnClickListener()
      {         
         @Override
         public void onClick(View v)
         {
            StringBuffer result = new StringBuffer();
            result.append(“Thanks : “).append(ch1.isChecked());
            result.append(“\nThanks: “).append(ch2.isChecked());
            Toast.makeText(MainActivity.this, result.toString(),
               Toast.LENGTH_LONG).show();
         }
      });
   }
}

Step 3: Now, it’s time to modify the default content of the activity_main.xml file. This file can be easily located under the res/layout/ directory. In here, you need to include the required Android UI controls (two TextView UI controls, two CheckBox UI controls, two Button UI controls, and one ImageButton).

<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:id=”@+id/textView1″
      android:layout_width=”wrap_content”
      android:layout_height=”wrap_content”
      android:text=”Example of checkbox”
      android:layout_alignParentTop=”true”
      android:layout_centerHorizontal=”true”
      android:textSize=”30dp” />      

   <CheckBox
      android:id=”@+id/checkBox1″
      android:layout_width=”wrap_content”
      android:layout_height=”wrap_content”
      android:text=” W3School”
      android:layout_above=”@+id/button”
      android:layout_centerHorizontal=”true” />      

   <CheckBox
      android:id=”@+id/checkBox2″
      android:layout_width=”wrap_content”
      android:layout_height=”wrap_content”
      android:text=”Android “
      android:checked=”false”
      android:layout_above=”@+id/checkBox1″
      android:layout_alignLeft=”@+id/checkBox1″
      android:layout_alignStart=”@+id/checkBox1″ />   

   <TextView
      android:id=”@+id/textView2″
      android:layout_width=”wrap_content”
      android:layout_height=”wrap_content”
      android:layout_alignLeft=”@+id/checkBox1″
      android:layout_below=”@+id/textView1″
      android:layout_marginTop=”39dp”
      android:text=”W3School”
      android:textColor=”#ff87ff09″
      android:textSize=”30dp”
      android:layout_alignRight=”@+id/textView1″
      android:layout_alignEnd=”@+id/textView1″ />      

   <Button
      android:layout_width=”wrap_content”
      android:layout_height=”wrap_content”
      android:text=”OK”
      android:id=”@+id/button”
      android:layout_alignParentBottom=”true”
      android:layout_alignLeft=”@+id/checkBox1″
      android:layout_alignStart=”@+id/checkBox1″ />      

   <Button
      android:layout_width=”wrap_content”
      android:layout_height=”wrap_content”
      android:text=”CANCEL”
      android:id=”@+id/button2″
      android:layout_alignParentBottom=”true”
      android:layout_alignRight=”@+id/textView2″
      android:layout_alignEnd=”@+id/textView2″ />      

   <ImageButton
      android:layout_width=”wrap_content”
      android:layout_height=”wrap_content”
      android:id=”@+id/imageButton”
      android:src=”@drawable/abc”
      android:layout_centerVertical=”true”
      android:layout_centerHorizontal=”true” />      
</RelativeLayout>

Step 4: Make sure you leave the strings.xml file untouched as the Android Studio takes care of the definitions of the above used constants. You can locate this file under the res/values/ folder.

<?xml version=”1.0″ encoding=”utf-8″?>
<resources>
   <string name=”app_name”>MyApplication</string>
</resources>

Step 5: Other than the strings.xml file, you don’t need to make any changes in the default content of the AndroidManifest.xml file. For you, we have mentioned its content here.

<?xml version=”1.0″ encoding=”utf-8″?>
<manifest xmlns:android=”http://schemas.android.com/apk/res/android”
   package=”com.example.myapplication” >   
   <application
      android:allowBackup=”true”
      android:icon=”@drawable/ic_launcher”
      android:label=”@string/app_name”
      android:theme=”@style/AppTheme” >      

      <activity
         android:name=”com.example.myapplication.MainActivity”
         android:label=”@string/app_name” >      
         <intent-filter>
            <action android:name=”android.intent.action.MAIN” />
            <category android:name=”android.intent.category.LAUNCHER” />
         </intent-filter>      
      </activity>      
   </application>
</manifest>

Step 6: To run this myapplication application, you need to open one of your project’s activity files in the Android Studio. But before that, make sure you have already created the AVD during the environment setup. Well, once you click on the Run button in the toolbar, your application will start loading on the AVD with the help of the Android Studio IDE. After the successful completion of the process, you will be able to see the final output on the Emulator window.

According to the final output, a user will be displayed with two CheckBox controls. One is labeled as “W3School” while the other is labeled as the “Android”. Once the user hits the OK button after selecting either of the options, he/she will receive a message saying “Thanks”. And in case, the user selects the Cancel button then the running application will be closed.