1. Creating New Project in Kotlin
- Open Android Studio.
- Go to File => New => New Project.Give Name To your application. Then, check Include Kotlin Support and click next button.
- Select minimum SDK you need. However, we have selected 21 as minimum SDK. Then, click next button.
- Then, select Empty Activity => click next => click finish.
2.Here We take Radio Group and inside Radio Group we take two Radio Button.We Provide Text to Radio Button Male and Female.
activity_main.xml
<?xml version="1.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="com.dharmendra.radiobutton.MainActivity"> <RadioGroup android:id="@+id/radioGroup" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_marginLeft="30dp" android:layout_marginTop="49dp" android:gravity="center"> <RadioButton android:id="@+id/male" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Male" /> <RadioButton android:id="@+id/female" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:text="Female" /> </RadioGroup> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/radioGroup" android:layout_centerHorizontal="true" android:layout_marginTop="56dp" android:text="Click" /> <TextView android:id="@+id/text" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_alignStart="@+id/radioGroup" android:layout_marginTop="22dp" android:text="Select Gender" android:textColor="@android:color/black" android:textStyle="bold" /> </RelativeLayout>
3.MainActivity.kt
package com.dharmendra.radiobutton import android.os.Bundle import android.support.v7.app.AppCompatActivity import android.widget.Button import android.widget.RadioButton import android.widget.RadioGroup import android.widget.Toast class MainActivity : AppCompatActivity() { //To Store Selected Value private var selection = "" override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) //FindViewByID for Radio Group and Button val radioGroup = findViewById<RadioButton>(R.id.radioGroup) as RadioGroup val btn=findViewById<Button>(R.id.button) as Button //On click For radioGroup radioGroup.setOnCheckedChangeListener { group, ID -> when (ID) { R.id.male -> { //If you Salected Male selection = "Male" } R.id.female -> { //If you Salected Female selection = "Female" } } } //OnClick For Button btn.setOnClickListener { if(selection.isEmpty()) Toast.makeText(this@MainActivity,"Please Select Any One",Toast.LENGTH_LONG).show() else Toast.makeText(this@MainActivity,"You have Selected "+selection,Toast.LENGTH_LONG).show() } } }
Output
No comments:
Post a Comment