Disable Android checkmark

The application is an application for a step sequencer with 16 groups of radio stations with 8 buttons in each group. It works great, unless the group has a selected button. I can’t turn it off if I don’t use the clear button that I created to clear all the radio groups. What I would like to add is a code that says that when the selected switch is selected, it just turns off like a switch. I tried using switches, but then other problems arose with this method. Below are two attempts, but both just stop me using the button

final RadioGroup radioGroup1 = (RadioGroup)findViewById(R.id.RadioGroup1); RadioButton D1 = (RadioButton)findViewById(R.id.RadioButtonD1); Button D1 = (Button)findViewById(R.id.RadioButtonD1); D1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { PdBase.sendFloat("D1", 74); int selectedTypeId = radioGroup1.getCheckedRadioButtonId(); RadioButton D1 = (RadioButton) findViewById(selectedTypeId); if(D1 != null) // This will be null if none of the radio buttons are selected radioGroup1.clearCheck(); PdBase.sendFloat("D1", 0); } }); RadioButton lC1 = (RadioButton)findViewById(R.id.RadioButtonlowC1); lC1.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { int selectedTypeId = radioGroup1.getCheckedRadioButtonId(); RadioButton lC1 = (RadioButton) findViewById(R.id.RadioButtonlowC1); if (selectedTypeId == -1){ PdBase.sendFloat("lC1", 72); } else if (selectedTypeId == R.id.RadioButtonlowC1) { radioGroup1.clearCheck(); PdBase.sendFloat("lC1", 0); } } }); 
+2
source share
5 answers

Recently, I had the same need - to have a radio group in which the selected item could be canceled by clicking on it again. I found that I was not able to accomplish this using listeners, but I was able to do this using a custom RadioButton , for example ...

 public class ToggleableRadioButton extends RadioButton { // Implement necessary constructors @Override public void toggle() { if(isChecked()) { if(getParent() instanceof RadioGroup) { ((RadioGroup)getParent()).clearCheck(); } } else { setChecked(true); } } } 

Notice that the button switches differently depending on its current state, i.e. it calls setChecked(true) on the button and calls clearCheck() in the group. If setChecked() used in both cases, the button that was simply canceled cannot be immediately re-selected - the logic in RadioGroup seems to cancel it immediately.

To use this button, simply replace the <RadioButton> tags with <your.package.ToggleableRadioButton> in your XML layout.

+14
source

I just used the answer @ spaaarky21

and my full code looks like this and it works great!

Java class

 public class ToggleableRadioButton extends RadioButton { public ToggleableRadioButton(Context context) { super(context); } public ToggleableRadioButton(Context context, AttributeSet attrs) { super(context, attrs); } public ToggleableRadioButton(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @TargetApi(Build.VERSION_CODES.LOLLIPOP) public ToggleableRadioButton(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { super(context, attrs, defStyleAttr, defStyleRes); } @Override public void toggle() { if(isChecked()) { if(getParent() instanceof RadioGroup) { ((RadioGroup)getParent()).clearCheck(); } } else { setChecked(true); } } } 

And for the XML layout

 <com.smart_dent.adapters.ToggleableRadioButton android:id="@+id/tejido_blando_perfil_convexo" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/tejido_blando_convexo_label" /> 

In this case, you just need to change the package, I can easily find it, it is on top of the Java Class Flie (if you created it from Android Studio)

+4
source

Actually, it can be done using listeners, but with OnTouchListener , which will fire before the button state changes, instead of the usual OnClickListener . The following works for me:

 View.OnTouchListener radioButtonOnTouchListener = new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { if (((RadioButton) v).isChecked()) { // If the button was already checked, uncheck them all radioGroup.clearCheck(); // Prevent the system from re-checking it return true; } return false; } }; radioButton1.setOnTouchListener(radioButtonOnTouchListener); radioButton2.setOnTouchListener(radioButtonOnTouchListener); 

Where radioGroup is the parent of radioButton1 and radioButton2

+3
source

Edit answer @ spaaarky21

 @Override public void toggle() { if (isChecked()) { if (getParent() instanceof RadioGroup) { ((RadioGroup) getParent()).clearCheck(); } // add else here when a single radioButton without radioGroup else { setChecked(false); } } else { setChecked(true); } } 
+1
source

This also does the job:

 public final class ToggleAbleRadioButton extends AppCompatRadioButton { public ToggleAbleRadioButton(final Context context, final AttributeSet attrs) { super(context, attrs); } @Override public void toggle() { setChecked(!isChecked()); } } 
0
source

All Articles