Android - setOnCheckedChangeListener for Radio Buttons

Android | RadioGroup.setOnCheckedChangeListener() Method: In this tutorial, we will learn how to work with the setOnCheckedChangeListener method for radio buttons in Android. By Manu Jemini Last updated : June 06, 2023

Android RadioGroup.setOnCheckedChangeListener() Method

setOnCheckedChangeListener is a method in android basically used with checkboxes, radio button etc. You can initiate this method easily like

While working with setOnCheckedChangeListener method in Android we and implement a listener with each checkboxes which gives us a callback function for each of them.

Syntax

public void setOnCheckedChangeListener(new CompoundButton. OnCheckedChangeListener)

There is also an important thing we must know while working with radio buttons is that we must wrap our button in RadioGroup which gives us a predefined way to uncheck the buttons.

After invoking this method a callback function will run. You can also initiate a class for more than one listener, so this can lead you to code reusability.

After making the class you can implement CompoundButton.OnCheckedChangeListener{} method which gives you an override method inherited form super class called onCheckedChanged(CompoundButton buttonView, boolean isChecked){} in which you can easily implement your code and it also gives you a button view and a Boolean isChecked() method.

Android RadioGroup.setOnCheckedChangeListener() Example

Java file

package com.example.hp.demo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    RadioButton male,female;

    TextView gender;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        male=(RadioButton)findViewById(R.id.male);
        female=(RadioButton)findViewById(R.id.female);

        gender=(TextView)findViewById(R.id.gender);

        male.setOnCheckedChangeListener(new Radio_check());
        female.setOnCheckedChangeListener(new Radio_check());
    }
    class Radio_check implements  CompoundButton.OnCheckedChangeListener
    {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if(male.isChecked())
            {
               gender.setText("Gender: Male");
            }
            else if(female.isChecked())
            {
                gender.setText("Gender: Female");
            }
        }
    }
}

XML file

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum="1">

    <RadioGroup
        android:layout_width="277dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.04">

        <RadioButton
            android:layout_width="205dp"
            android:layout_height="56dp"
            android:text="Male"
            android:id="@+id/male"
            android:checked="false" />

        <RadioButton
            android:layout_width="194dp"
            android:layout_height="57dp"
            android:text="Female"
            android:id="@+id/female"
            android:checked="false" />
    </RadioGroup>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/gender"
        android:layout_weight="0.16"
        android:hint="Gender" />
</LinearLayout>

Manifest file

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.hp.demo">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Output

setOnCheckedChangeListener for Radio Buttons
setOnCheckedChangeListener for Radio Buttons

This is the way we do it, thank you.



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.