Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android unchecked radio button which is already checked

<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rg"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.radiogroup.MainActivity" >

    <RadioButton
        android:id="@+id/rb_true"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center_vertical" />

    <RadioButton
        android:id="@+id/rb_false"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center_vertical" />

</RadioGroup>

i have one radio group in which i have 2 radio button

now if radio button 1 is selected and if i tap on same radio button(radio button 1) it should be unchecked ..

it should work astoggle.

like image 954
Query Avatar asked Nov 01 '22 04:11

Query


2 Answers

This should serve the purpose.

radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener()
    {
    @Override
    public void onCheckedChanged(RadioGroup arg0, int id)
    {
    RadioButton checkedRadioButton = (RadioButton)radioGroup.findViewById(id);

    boolean isChecked = checkedRadioButton.isChecked();

    if (isChecked)
    {
    Toast.makeText(getApplicationContext(), 
               checkedRadioButton.getText(),Toast.LENGTH_LONG).show();
    radioButton = (String) checkedRadioButton.getText();
    checkedRadioButton.setChecked(false);
    }
    else
        checkedRadioButton.setChecked(true);
    }});

Also, don't forget to initialize RadioGroup

like image 160
Deepika Rajani Avatar answered Nov 11 '22 11:11

Deepika Rajani


Yes.. It is possible that radio button can work like checkbox

First Declare flags "false"

private boolean flagmale = false;
private boolean flagfemale = false;


rdbtnMale.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (rdbtnMale.isChecked()) {
                if (!flagmale) {
                    rdbtnMale.setChecked(true);
                    rdbtnFemale.setChecked(false);
                    flagmale = true;
                    flagfemale = false;
                } else {
                    flagmale = false;
                    rdbtnMale.setChecked(false);
                    rdbtnFemale.setChecked(false);
                }
            }
        }
    });


    rdbtnFemale.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (rdbtnFemale.isChecked()) {
                if (!flagfemale) {
                    rdbtnFemale.setChecked(true);
                    rdbtnMale.setChecked(false);
                    flagfemale = true;
                    flagmale = false;
                } else {
                    flagfemale = false;
                    rdbtnFemale.setChecked(false);
                    rdbtnMale.setChecked(false);
                }
            }
        }
    });

This is also possible with one flag..

like image 26
MashukKhan Avatar answered Nov 11 '22 11:11

MashukKhan