RadioButton in KMP KMM Compose Multiplatform

In today’s tutorial, we will learn about Integrating RadioButton in KMP KMM Compose Multiplatform for both Android & iOS platforms. In this step-by-step guide, we will retrieve the Radio button selected item, Customize the radio button Color, Size, Update the button selected item color, and implement disabled radio buttons. So let’s get started.

RadioButton in KMP KMM Compose Multiplatform

RadioButton in KMP KMM Kotlin Compose Multiplatform:

A Radio button is a circular button that can be selected when tapping and de-selected when its other items are pressed. Radio buttons are typically defined as part of the group. It is useful when the user needs to pick a single value from multiple choices. It ensures users only select one value at a time.

Implement Radio Button in KMP:

To integrate RadioButton in KMP Kotlin Compose Multiplatform, we will use its predefined RadioButton component.

Code explanation:

  • selected: Contains the selected or not selected State.
  • onClick: Calls every time the user taps on the radio button.

Source code:

Screenshot:

Radio Button in KMP

Adding lable to Radio button:

Adding a Label Title on the Radio button in KMP is very easy. We have to put the Radio Button in the Row widget and put the Text widget with it.

Source code:

Screenshot:

Radio button with Label

Customizing Radio button:

To customize radio button colours, a property RadioButtonDefaults.colors() is avaible. This property has 3 sub-properties:

  1. selectedColor: Color of Radio button when active – selected.
  2. unselectedColor: Color of Radio button when inactive or unselected.
  3. disabledColor: Disabled Radio button color.

Screenshot:

Customize Radio Button

 

Disabled Radio button:

To create a disabled radio button in KMP, we have to use the enabled property. It supports True and False values.

Screenshot:

Disabled Radio Button in KMP

 

Implement Multiple Radio button(Radio Group) together:

To create a Radio group, first, we must make a State with multiple string items. These items contain Radio button items. We also have to make a selected item state that contains the index position of the radio button selected item. Ultimately, we will use the ForEach loop to render all the items.

 

Complete source code for App.kt file:

Screenshot in Android device:

RadioButton in KMP KMM Compose Multiplatform

Screenshot in iOS device:

RadioButton in KMP KMM Compose Multiplatform

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *