3/9/2021 CheckBox in Kotlin - GeeksforGeeks
Related Articles
CheckBox in Kotlin
Difficulty Level : Medium ● Last Updated : 19 Feb, 2021
A CheckBox is a special kind of button in Android which has two states either checked or unchecked. The Checkbox is a very common widget to be used in Android and a very good example is the “Remember me” option in any kind of Login activity of an app which asks the user to activate or deactivate that service. There are many other uses of the CheckBox widget like offering a list of options to the user to choose from and the options are mutually exclusive i.e., the user can select more than one option. This feature of the CheckBox makes it a better option to be used in designing multiple-choice questions application or survey application in android.
Class hierarchy of CheckBox class in Kotlin
XML attributes of CheckBox widget We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Got It ! Policy https://www.geeksforgeeks.org/checkbox-in-kotlin/ 1/10 3/9/2021 CheckBox in Kotlin - GeeksforGeeks
XML Attributes Description
android:id Used to uniquely identify a CheckBox
android:checked To set the default state of a CheckBox as checked or unchechek
android:background To set the background color of a CheckBox
android:text Used to store a text inside the CheckBox
android:fontFamily To set the font of the text of the CheckBox
android:textSize To set the CheckBox text size
android:layout_width To set the CheckBox width
android:layout_height To set the CheckBox height
android:gravity Used to adjust the CheckBox text alignment
android:padding Used to adjust the left, right, top and bottom padding of the CheckBox
Example
This example demonstrates the steps involved in designing an activity that consists of 5 CheckBox and an additional submit button to display a toast message that user response has been recorded.
Note: Following steps are performed on Android Studio version 4.0
We use cookies to ensure you have the best browsing experience on our website. By using Courre site,ate you ne acknowledgew project that you have read and understood our Cookie Policy & Privacy Got It ! Policy https://www.geeksforgeeks.org/checkbox-in-kotlin/ 2/10 3/9/2021 CheckBox in Kotlin - GeeksforGeeks 1. Click on File, then New => New Project. 2. Choose “Empty Activity” for the project template. 3. Select language as Kotlin. 4. Select the minimum SDK as per your need.
Open activity_main.xml file
Below is the code for activity_main.xml file to add 5 CheckBox. A normal “submit” button is also added to display a toast message that user response has been recorded.
Open MainActivity.kt file
Below is the code for MainActivity.kt file to access CheckBox widget in kotlin file and show a proper message whenever the submit button is clicked by the user.
package com.example.checkboxinkotlin
import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import android.widget.Button import android.widget.Toast
class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main)
// Assigning id of the submit button W e use cookies val to bensureutto nyou : have But theto bestn = browsingfindV iexperienceewById( onR. iourd. website.submi tByBu usingtton) our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Got It ! // Actions to be perPolicyformed https://www.geeksforgeeks.org/checkbox-in-kotlin/ 5/10 3/9/2021 CheckBox in Kotlin - GeeksforGeeks // when Submit button is clicked button.setOnClickListener{
// Display toast message Toast.makeText(applicationContext, "Your response has been recorded", Toast.LENGTH_LONG).show() } } }
Modify strings.xml file All the strings which are used in the activity, from the text view to the Checkbox texts are listed in this file.
AndroidManifest.xml file
Below is the code for AndroidManifest.xml file.
Output
Like 0
Previous Next
RECOMMENDED ARTICLES Page : 1 2 3
01 How to use CheckBox in Android 05 How to set checkbox size in We use cookies07, Au tog 1ensure8 you have the best browsing experience on our website.HTML By/C usingSS? our site, you acknowledge that you have read and understood our Cookie2 Policy6, Feb & 1 Privacy9 Got It ! Policy https://www.geeksforgeeks.org/checkbox-in-kotlin/ 7/10 3/9/2021 CheckBox in Kotlin - GeeksforGeeks How to style a checkbox using CSS? How to create an HTML checkbox 02 13, Feb 19 06 with a clickable label? 10, Apr 19
How to read if a checkbox is How to check whether a checkbox 03 checked in PHP? 07 is checked in jQuery? 29, Oct 18 28, Apr 19
How to Check/Uncheck the jQuery | :checkbox Selector 08 checkbox using JavaScript ? 04 27, Feb 19 27, Sep 19
Article Contributed By :
RISHU_MISHRA @RISHU_MISHRA
Vote for di culty Current di culty : Medium
Easy Normal Medium Hard Expert
Article Tags : Android-Button , Kotlin Android , Picked , Android , Kotlin
Practice Tags : Android
Improve Article Report Issue
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Got It ! Policy https://www.geeksforgeeks.org/checkbox-in-kotlin/ 8/10 3/9/2021 CheckBox in Kotlin - GeeksforGeeks Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.
0 Comments GeeksforGeeks Disqus' Privacy Policy 1 Login
Recommend t Tweet f Share Sort by Newest
Start the discussion…
LOG IN WITH OR SIGN UP WITH DISQUS ?
Name
Be the first to comment.
✉ Subscribe d Add Disqus to your siteAdd DisqusAdd ⚠ Do Not Sell My Data
5th Floor, A-118, Sector-136, Noida, Uttar Pradesh - 201305
Company Learn
About Us Algorithms
Careers Data Structures
Privacy Policy Languages
Contact Us CS Subjects
Copyright Policy Video Tutorials
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Got It ! Practice Policy Contribute https://www.geeksforgeeks.org/checkbox-in-kotlin/ 9/10 3/9/2021 CheckBox in Kotlin - GeeksforGeeks Courses Write an Article
Company-wise Write Interview Experience
Topic-wise Internships
How to begin? Videos
@geeksforgeeks , Some rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Got It ! Policy https://www.geeksforgeeks.org/checkbox-in-kotlin/ 10/10