<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.
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
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..
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With