In this article, we will learn about android Radio Group using Kotlin. We will go through various example that demonstrates how to use different attributes of Radio Group. For example, …..
In this article, we will get answer to questions like –
- What is Radio Group?
- Why should we consider Radio Group while designing ui for any app?
- What are possibilities using Radio Group while designing ui? etc.
Let’s have a quick demo of things we want to cover in this tutorial –
Output
Getting Started
We can define android Radio Group widget as below –
Radio Group is used to create a multiple-exclusion scope for a set of radio buttons i.e. You can select only one radio button out of all radio buttons present in any radio group. Selecting one radio button automatically de-select previously selected radio button.
Now, how do we use Radio Group in android application ?
Creating New Project
At first, we will create an application.
So, follow steps below to create any android project in Kotlin –
Step | Description |
---|---|
1. | Open Android Studio (Ignore if already done). |
2. | Go to File => New => New Project. This will open a new window. Then, under Phone and Tablet section, select Empty Activity. Then, click Next. |
3. | In next screen, select project name as WIDGET_PROJECT_NAME. Then, fill other required details. |
4. | Then, clicking on Finish button creates new project. |
Newbie in Android ?
Some very important concepts (Recommended to learn before you move ahead)
Before we move ahead, we need to setup for viewBinding to access Android Radio Group Using Kotlin file without using findViewById() method.
Setup ViewBinding
Add viewBinding true in app/build.gradle file.
android { // OTHER CODE... buildFeatures { viewBinding true } }
Now, set content in activity using view binding.
Open MainActivity.kt file and write below code in it.
class MainActivity : AppCompatActivity() { private lateinit var binding: ActivityMainBinding override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) binding = ActivityMainBinding.inflate(layoutInflater) val view = binding.root setContentView(view) } }
Now, we can access view in Kotlin file without using findViewById() method.
Using Radio Group in Kotlin
Follow steps below to use Radio Group in newly created project –
- Open res/values/strings.xml file. Then, add below code into it –
<resources> <string name="app_name">RadioGroup</string> <string name="male">Male</string> <string name="female">Female</string> </resources>
- Open res/layout/activity_main.xml file. Then, add below code in it –
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <RadioGroup android:id="@+id/radioGroup" android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingStart="20dp" android:paddingEnd="20dp" app:layout_constraintTop_toTopOf="parent" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent"> <RadioButton android:id="@+id/radioMale" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="10dp" android:text="@string/male"/> <RadioButton android:id="@+id/radioFemale" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="10dp" android:text="@string/female"/> </RadioGroup> </androidx.constraintlayout.widget.ConstraintLayout>
-
We can also access it in Kotlin File, MainActivity.kt, as below –
package com.tutorialwing.radiogroup import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import android.widget.Toast import com.tutorialwing.radiogroup.databinding.ActivityMainBinding class MainActivity : AppCompatActivity() { private lateinit var binding: ActivityMainBinding override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) binding = ActivityMainBinding.inflate(layoutInflater) val view = binding.root setContentView(view) setOnCheckedChangeListener() } private fun setOnCheckedChangeListener() { binding.radioGroup.setOnCheckedChangeListener { group, checkedId -> val text = "You selected: " + if (R.id.radioMale == checkedId) "male" else "female" Toast.makeText(applicationContext, text, Toast.LENGTH_SHORT).show() } } }
Now, run the application. We will get output as below –
Different Attributes of Radio Group in XML
Now, we will see how to use different attributes of Android Radio Group using Kotlin to customise it –
Set Id of Radio Group
Many a time, we need id of View to access it in kotlin file or create ui relative to that view in xml file. So, we can set id of Radio Group using android:id attribute like below –
<RadioGroup android:id="@+id/radioGroup_ID" />
Here, we have set id of Radio Group as radioGroup_ID using android:id=”” attribute. So, if we need to reference this Radio Group, we need to use this id – radioGroup_ID.
Learn to Set ID of Radio Group Dynamically
Set Width of Radio Group
We use android:layout_width=”” attribute to set width of Radio Group.
We can do it as below –
<RadioGroup android:id="@+id/radioGroup_ID" android:layout_width="wrap_content" />
Width can be either “MATCH_PARENT” or “WRAP_CONTENT” or any fixed value (like 20dp, 30dp etc.).
Learn to Set Width of Radio Group Dynamically
Set Height of Radio Group
We use android:layout_height=”” attribute to set height of Radio Group.
We can do it as below –
<RadioGroup android:id="@+id/radioGroup_ID" android:layout_width="wrap_content" android:layout_height="wrap_content" />
Height can be either “MATCH_PARENT” or “WRAP_CONTENT” or any fixed value.
Learn to Set Height of Radio Group Dynamically
Set Padding of Radio Group
We use android:padding=”” attribute to set padding of Radio Group.
We can do it as below –
<RadioGroup android:id="@+id/radioGroup_ID" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="10dp" />
Here, we have set padding of 10dp in Radio Group using android:padding=”” attribute.
Learn to Set Padding of Radio Group Dynamically
Set Margin of Radio Group
We use android:layout_margin=”” attribute to set margin of Radio Group.
We can do it as below –
<RadioGroup android:id="@+id/radioGroup_ID" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="10dp" />
Here, we have set margin of 10dp in Radio Group using android:layout_margin=”” attribute.
Learn to Set Margin of Radio Group Dynamically
Set Background of Radio Group
We use android:background=”” attribute to set background of Radio Group.
We can do it as below –
<RadioGroup android:id="@+id/radioGroup_ID" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#ff0000" />
Here, we have set background of color #ff0000 in Radio Group using android:background=”” attribute.
Learn to Set Background of Radio Group Dynamically
Set Visibility of Radio Group
We use android:visibility=”” attribute to set visibility of Radio Group.
We can do it as below –
<RadioGroup android:id="@+id/radioGroup_ID" android:layout_width="wrap_content" android:layout_height="wrap_content" android:visibility="gone" />
Here, we have set visibility of Radio Group using android:visiblity=”” attribute. Visibility can be of three types – gone, visible and invisible
Learn to Set Visibility of Radio Group Dynamically
Set Text of Radio Group
We use android:text=”” attribute to set text of Radio Group.
We can do it as below –
<RadioGroup android:id="@+id/radioGroup_ID" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello Tutorialwing" />
Here, we have set text (“Hello Tutorialwing”) in Radio Group using android:text=”” attribute.
Similarly, we can set any text using this attribute.
Learn to Set Text of Radio Group Dynamically
Set Color of Text in Radio Group
We use android:textColor=”” attribute to set color of text in Radio Group.
We can do it as below –
<RadioGroup android:id="@+id/radioGroup_ID" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello Tutorialwing" android:textColor="#ffffff" />
Here, we have set color (#ffffff i.e. white) of text (“Hello Tutorialwing”) in Radio Group using android:textColor=”” attribute. Similarly, we can set any color using this attribute.
Learn to Set Color of Radio Group Dynamically
Set Gravity of Radio Group
We use android:gravity=”” attribute to set gravity of text in Radio Group.
We can do it as below –
<RadioGroup android:id="@+id/radioGroup_ID" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello Tutorialwing" android:gravity="center_horizontal" />
Here, we have set gravity of text in Radio Group using android:gravity=”” attribute. Attribute value can be – “center_horizontal”, “center”, “center_vertical” etc.
Learn to Set Gravity of Radio Group Dynamically
Set Text in Uppercase, Lowercase
If we need to show text of Radio Group in uppercase or lowercase etc.
Set text in uppercase
We can use android:textAllCaps=”true” attribute to set text in uppercase. We can do it as below –
<RadioGroup android:id="@+id/radioGroup_ID" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello Tutorialwing" android:textAllCaps="true" />
Attribute android:textAllCaps=”true” sets text in uppercase. So, HELLO TUTORIALWING is set in Radio Group.
By default, false is set in this attribute. So, Whatever value is written in android:text=”” attribute, it will be set as it is. For example,
<RadioGroup android:id="@+id/radioGroup_ID" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello Tutorialwing" android:textAllCaps="false" />
Above code will set Hello Tutorialwing to Radio Group.
How do we set text in lowercase?
Answer –
- In xml file – write all the text in lowercase.
- In kotlin file – take text as string. Then, convert it in lowercase. Then, set it to Radio Group.
Learn to Set Text in Uppercase or Lowercase Dynamically
Set Size of Text in Radio Group
We use android:textSize=”” attribute to set size of text in Radio Group.
We can do it as below –
<RadioGroup android:id="@+id/radioGroup_ID" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello Tutorialwing" android:textSize="20sp" />
Here, we have set size of text in Radio Group using android:textSize=”” attribute.
Learn to Set Size of Text of Radio Group Dynamically
Set Style (Bold/italic) of Text in Radio Group
We use android:textStyle=”” attribute to set style (bold, italic etc.) of text in Radio Group.
We can do it as below –
<RadioGroup android:id="@+id/radioGroup_ID" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello Tutorialwing" android:textStyle="bold" />
Here, we have set style of text in Radio Group using android:textStyle=”” attribute. This attribute can take bold, italic or normal.
Learn to Set Style of Text of Radio Group Dynamically
Set Letter Spacing of Text in Radio Group
We use android:letterSpacing=”” attribute to set spacing between letters of text in Radio Group.
We can do it as below –
<RadioGroup android:id="@+id/radioGroup_ID" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello Tutorialwing" android:letterSpacing="1" />
Here, we have set spacing between letters of text in Radio Group using android:letterSpacing=”” attribute.
Learn to Set Letter Spacing of Text of Radio Group Dynamically
Set Typeface of Text in Radio Group
We use android:typeface=”” attribute to set typeface in Radio Group.
We can do it as below –
<RadioGroup android:id="@+id/radioGroup_ID" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello Tutorialwing" android:typeface="sans" />
Here, we have set typeface of text in Radio Group using android:typeface=”” attribute. This attribute can take values – “sans”, “normal”, “monospace” or “normal”.
Learn to Set Typeface of Radio Group Dynamically
Set fontFamily of Text in Radio Group
We use android:fontFamily=”” attribute to set fontFamily of text in Radio Group.
We can do it as below –
<RadioGroup android:id="@+id/radioGroup_ID" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello Tutorialwing" android:fontFamily="sans-serif" />
Here, we have set fontFamily (Here, sans-serif) of text in Radio Group using android:fontFamily=”sans-serif” attribute.
Till now, we have see how to use android Radio Group using Kotlin. We have also gone through different attributes of Radio Group to perform certain task. Let’s have a look at list of such attributes and it’s related task.
Different Attributes of Android Radio Group Widget
Below are the various attributes that are used to customise android Radio Group Widget. However, you can check the complete list of attributes of Radio Group in it’s official documentation site. Here, we are going to list some of the important attributes of this widget –
Some of the popular attributes of Radio Group inherited from LinearLayout are –
Sr. | XML Attributes | Description |
---|---|---|
1 | android:baselineAligned | Set whether to align RadioGroup’s children baselines |
2 | android:divider | Set drawable to be used as a vertical divider between buttons |
3 | android:gravity | Defines how an object will be aligned within layout |
4 | android:orientation | Specifies how the object should be aligned within this layout. For example, horizontal or vertical etc. |
5 | android:weightSum | Specifies maximum weight sum |
Some of the popular attributes of android Radio Group inherited from ViewGroup are –
Sr. | XML Attributes | Description |
---|---|---|
1 | android:animationCache | Specifies whether layout animations should create a drawing cache for their children |
2 | android:clipChildren | Specifies whether a child is limited to draw inside of its bounds or not. |
3 | android:layoutMode | Specifies the layout mode of this ViewGroup. |
Some of the popular attributes of android Radio Group inherited from View are –
Sr. | XML Attributes | Description |
---|---|---|
1 | android:alpha | Specifies alpha of the view |
2 | android:background | Specifies background of the view |
3 | android:backgroundTint | Specifies background tint |
4 | android:clickable | Specifies whether view is clickable or not |
5 | android:id | Specifies id of the view. Note- Id must be unique within an xml file |
We have seen different attributes of Radio Group and how to use it. If you wish to visit post to learn more about it
Thus, we have seen what is Radio Group, how can we use android Radio Group using Kotlin ? etc. We also went through different attributes of android Radio Group.
You must be logged in to post a comment.