How To Set Radio Button Checked In Android Programmatically
In Android, we use Radio buttons when we have to select one selection from the listing of available options. A RadioButton has 2 states. A RadioButton can be checked or unchecked by the user. By default, the RadioButton has OFF (Unchecked State). So, if a user clicks a RadioButton information technology comes in a checked state then it cannot be unchecked by clicking it again. A checked RadioButton becomes unchecked when you click any other RadioButton in the aforementioned Radio Group. Then, let'south understand Radio Push button in Android with Kotlin linguistic communication.
The reward of using Radio Buttons is that a user can create a listing of available options. All these options are available to the user but the user tin select (cheque) simply one option from the listing.
We use Radio Buttons together using RadioGroup widget. RadioGroup is responsible of group Radio Buttons and it provides the feature to select one Radio Button at a time. If a RadioButton is in checked country and any other RadioButton is clicked inside the same grouping then the previously checked RadioButton becomes unchecked.
Radio Button Android Kotlin:
ane- Create a new Android Studio Project. Name your project, then in my instance, the name is "RadioButtonKotlin". Select any API level co-ordinate to your choice and cull your default language equally KOTLIN.
2- In your activity_main.xml alter the default Constraint Layout to Relative Layout. Hither we add a TextView to brandish some text and a RadioGroup.
| 1 2 3 4 five 6 seven 8 9 10 11 12 13 14 15 sixteen 17 xviii | < TextView android : layout_width = "wrap_content" android : layout_height = "wrap_content" android : layout_centerHorizontal = "true" android : layout_marginTop = "130dp" android : text = "Select Your Pick" android : textColor = "#000000" android : textSize = "30sp" / > < RadioGroup android : id = "@+id/radiogroup" android : layout_width = "match_parent" android : layout_height = "wrap_content" android : layout_centerHorizontal = "truthful" android : layout_marginTop = "180dp" android : padding = "16dp" > |
3- Inside our Radio Group we will take five Radio Buttons. We add text for our Radio Buttons and assign ids to them. We take put 1 Radio Button in a checked country by default.
| 1 2 iii iv 5 6 7 eight 9 10 eleven 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 thirty 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | < RadioButton android : id = "@+id/radio_1" android : layout_width = "wrap_content" android : layout_height = "wrap_content" android : text = "Java" android : textSize = "25sp" android : textColor = "#000000" / > < RadioButton android : id = "@+id/radio_2" android : layout_width = "wrap_content" android : layout_height = "wrap_content" android : layout_marginTop = "10dp" android : text = "Kotlin" android : textSize = "25sp" android : textColor = "#000000" / > < RadioButton android : id = "@+id/radio_3" android : layout_width = "wrap_content" android : layout_height = "wrap_content" android : layout_marginTop = "10dp" android : text = "Python" android : textSize = "25sp" android : textColor = "#000000" / > < RadioButton android : id = "@+id/radio_4" android : layout_width = "wrap_content" android : layout_height = "wrap_content" android : layout_marginTop = "10dp" android : text = "Swift" android : textSize = "25sp" android : textColor = "#000000" android : checked = "true" / > < RadioButton android : id = "@+id/radio_5" android : layout_width = "wrap_content" android : layout_height = "wrap_content" android : layout_marginTop = "10dp" android : text = "Javascript" android : textSize = "25sp" android : textColor = "#000000" / > |
4- Finally, the complete activity_main.xml with a TextView, RadioGroup and five Radio Buttons will look like this:
activity_main.xml
| 1 2 iii 4 v half-dozen seven 8 9 ten 11 12 thirteen 14 15 16 17 xviii nineteen 20 21 22 23 24 25 26 27 28 29 thirty 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 | <? xml version = "i.0" encoding = "utf-8" ?> < 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" tools : context = ".MainActivity" > < TextView android : layout_width = "wrap_content" android : layout_height = "wrap_content" android : layout_centerHorizontal = "true" android : layout_marginTop = "130dp" android : text = "Select Your Pick" android : textColor = "#000000" android : textSize = "30sp" / > < RadioGroup android : id = "@+id/radiogroup" android : layout_width = "match_parent" android : layout_height = "wrap_content" android : layout_centerHorizontal = "truthful" android : layout_marginTop = "180dp" android : padding = "16dp" > < RadioButton android : id = "@+id/radio_1" android : layout_width = "wrap_content" android : layout_height = "wrap_content" android : text = "Java" android : textSize = "25sp" android : textColor = "#000000" / > < RadioButton android : id = "@+id/radio_2" android : layout_width = "wrap_content" android : layout_height = "wrap_content" android : layout_marginTop = "10dp" android : text = "Kotlin" android : textSize = "25sp" android : textColor = "#000000" / > < RadioButton android : id = "@+id/radio_3" android : layout_width = "wrap_content" android : layout_height = "wrap_content" android : layout_marginTop = "10dp" android : text = "Python" android : textSize = "25sp" android : textColor = "#000000" / > < RadioButton android : id = "@+id/radio_4" android : layout_width = "wrap_content" android : layout_height = "wrap_content" android : layout_marginTop = "10dp" android : text = "Swift" android : textSize = "25sp" android : textColor = "#000000" android : checked = "truthful" / > < RadioButton android : id = "@+id/radio_5" android : layout_width = "wrap_content" android : layout_height = "wrap_content" android : layout_marginTop = "10dp" android : text = "Javascript" android : textSize = "25sp" android : textColor = "#000000" / > < / RadioGroup > < / RelativeLayout > |
five- So, now in our MainActivity.kt file nosotros simply add onClickListener on our Radio Buttons. With the assistance of an onClickListener we will display a Toast bulletin. We set up onClickListener in our onCreate function.
| 1 2 3 iv 5 half-dozen seven 8 9 10 11 12 13 fourteen 15 16 17 18 nineteen | radio_1 . setOnClickListener { Toast . makeText ( this , "Y'all Selected: Coffee" , Toast . LENGTH_SHORT ) . show ( ) } radio_2 . setOnClickListener { Toast . makeText ( this , "You Selected: Kotlin" , Toast . LENGTH_SHORT ) . show ( ) } radio_3 . setOnClickListener { Toast . makeText ( this , "You Selected: Python" , Toast . LENGTH_SHORT ) . show ( ) } radio_4 . setOnClickListener { Toast . makeText ( this , "You Selected: Swift" , Toast . LENGTH_SHORT ) . testify ( ) } radio_5 . setOnClickListener { Toast . makeText ( this , "You Selected: Javascript" , Toast . LENGTH_SHORT ) . show ( ) } |
6- Then our complete MainActivity.kt file volition exist like this:
MainActivity.kt
| 1 ii iii 4 five 6 7 viii 9 10 11 12 13 14 15 16 17 xviii nineteen 20 21 22 23 24 25 26 27 28 29 thirty 31 32 33 34 35 | package com . example . radiobuttonkotlin import androidx . appcompat . app . AppCompatActivity import android . os . Bundle import android . widget . Toast import kotlinx . android . synthetic . main . activity_main . * form MainActivity : AppCompatActivity ( ) { override fun onCreate ( savedInstanceState : Bundle ? ) { super . onCreate ( savedInstanceState ) setContentView ( R . layout . activity_main ) radio_1 . setOnClickListener { Toast . makeText ( this , "You Selected: Coffee" , Toast . LENGTH_SHORT ) . show ( ) } radio_2 . setOnClickListener { Toast . makeText ( this , "You Selected: Kotlin" , Toast . LENGTH_SHORT ) . prove ( ) } radio_3 . setOnClickListener { Toast . makeText ( this , "You lot Selected: Python" , Toast . LENGTH_SHORT ) . show ( ) } radio_4 . setOnClickListener { Toast . makeText ( this , "You Selected: Swift" , Toast . LENGTH_SHORT ) . bear witness ( ) } radio_5 . setOnClickListener { Toast . makeText ( this , "You Selected: Javascript" , Toast . LENGTH_SHORT ) . show ( ) } } } |
And so, this is information technology for our Radio Button Android Kotlin example. I hope at present y'all accept learned how to use Radio Button in Android Studio only nevertheless, if you have any queries regarding Android Radio Push then yous can definitely ask in the comments department beneath.
OUTPUT
…
Radio Button Android Kotlin: Android Studio Kotlin ExampleRead More »
How To Set Radio Button Checked In Android Programmatically,
Source: https://myandroid.site/tag/how-to-set-radio-button-checked-in-android-programmatically/
Posted by: howarthyesquir.blogspot.com

0 Response to "How To Set Radio Button Checked In Android Programmatically"
Post a Comment