Hello Readers! In this tutorial, we will learn about how to use android checkbox with example. We will learn about different attributes that are used to customise this widget.
Output
Getting Started
Android Checkbox can be described as below –
Android Checkbox is special type of button that has only 2 states i.e. either checked or unchecked.
Different Attributes of Android Checkbox Widget
Attributes of checkbox widget are inherited from Textview, View and Compound Button. Some of the popular attributes inherited from Textview are –
Sr. | XML Attributes | Description |
---|---|---|
1 | android:backgroundTint | Sets tint to the background. |
2 | android:clickable | Sets true when you want to make View clickable. Otherwise, set false. |
3 | android:drawableBottom | Drawable to be drawn at bottom of the text. |
4 | android:drawableEnd | Drawable to be drawn to end of the text. |
5 | android:drawableLeft | Drawable to be drawn to left of the text. |
6 | android:drawablePadding | Padding of the drawable. |
Attributes of Checkbox inherited from Compound Button are –
Sr. | XML Attributes | Description |
---|---|---|
1 | android:button | Drawable to be used for button graphic |
2 | android:buttonTint | Sets tint to button graphic |
3 | android:buttonTintMode | Blending mode used to apply the button graphic tint. |
Attributes of Checkbox inherited from View are –
Sr. | XML Attributes | Description |
---|---|---|
1 | android:id | It sets unique identifier for this view. |
2 | android:padding | Sets padding of this view. |
3 | android:onClick | Defines the operations to perform when this view is clicked |
4 | android:visibility | Sets the visibility (visible, gone etc.) of the Checkbox. |
5 | android:tooltipText | Defines text displayed in a small popup window on hover or long press. |
6 | android:background | Sets background to this view. |
7 | android:alpha | Sets alpha in view. |
Example of Android Checkbox Widget
At first, we will create android application. Then, we will use checkbox widget in this application.
1. Creating New Project
Follow steps below to create new android project. Please ignore the steps if you have already created android application.
Step | Description |
---|---|
1. | Open Android Studio. |
2. | Go to File => New => New Project. Write application name as Checkbox. Then, click next button. |
3. | Select minimum SDK you need. However, we have selected 17 as minimum SDK. Then, click next button |
4. | Then, select Empty Activity => click next => click finish. |
5. | If you have followed above process correctly, you will get a newly created project successfully. However, you can also visit post to create a new project to know steps in detail. |
Now, we will modify xml and java file to modify or perform some action when checkbox is clicked. Please follow the steps below.
2. Modify Values folder
Open res/values/strings.xml file. Then, add below code into it.
<resources> <string name="app_name">Checkbox</string> <string name="check_it">Check it</string> </resources>
Other values folder have not been changed. So, we are not going to mention it here.
3. Use Checkbox in xml file
Open res/layout/activity_main.xml file. Then, add below code into it.
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center"> <CheckBox android:id="@+id/checkBox" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="10dp" android:text="@string/check_it"/> </LinearLayout>
Notice that we have defined Checkbox widget with id checkBox in xml file. Now, we will access this checkbox widget in java file. Then, We will show a toast message when checkbox is clicked.
4. Access Checkbox in java file
Open src/main/java/com.tutorialwing.checkbox/MainActivity.java file. Then, add below code into it.
package com.tutorialwing.checkbox; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.Toast; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); CheckBox checkBox = findViewById(R.id.checkBox); if (checkBox != null) { checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { String msg = "You have " + (isChecked ? "checked" : "unchecked") + " this Check it Checkbox."; Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show(); } }); } } }
Notice that we have added a listener whenever checked/unchecked state is changed in checkbox.
Since AndroidManifest file is very important for any android application. We are also going to mention it here.
AndroidManifest.xml file
Code inside main/AndroidManifest.xml file is as below.
<?xml version="1.0" encoding="utf-8"?> <manifest package="com.tutorialwing.checkbox" xmlns:android="http://schemas.android.com/apk/res/android"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.LAUNCHER"/> </intent-filter> </activity> </application> </manifest>
When you run the application, you will get output as shown above.
That’s the end of tutorial on Android Checkbox widget.
You must be logged in to post a comment.