I made a custom RadioButton that looks as follow in a Android 5.0 device.
These RadioButtons are dynamic created as shown in the follow methods. So the first method redioButtonPresenterApparence sets its appearance removing circle (setting buttonDrwable to null. The second method set the buttons background later.
private void radioButtonPresenterApparence(RadioButton presenter, int icon) {
Drawable drawable = getResources().getDrawable(icon);
presenter.setCompoundDrawablesWithIntrinsicBounds(drawable, null, null, null);
presenter.setButtonDrawable(null);
presenter.setGravity(Gravity.CENTER);
}
private void updateButtonsBackground(){
int childCount = getChildCount();
BackgroundSelector bgSelector = new BackgroundSelector(childCount);
for(int i = 0; i < childCount; i++){
View rb = getChildAt(i);
rb.setBackgroundResource( bgSelector.getBackgroundResource(i) );
rb.requestLayout();
}
requestLayout();
}
My problem is when testing the same on Samsung Android 4.4.4 devices (not sure about other manufactories), it shows as follow.
PS: It's a code created RadioButton. You can check it in the follow method:
private void addPresenter(int icon){
RadioButton presenter = new RadioButton(getContext()); //Create new RadioButton
radioButtonPresenterApparence(presenter, icon); //Set icon and configure aparence
addView(presenter); //Add new button to Selector
presenterParentAparance(presenter); //Config button inside parent
requestLayout(); //Request layout update to Selector
}
Android RadioButton has only two states: checked or unchecked. Apart from these, the radio button can't have any other value. You can't uncheck a checked RadioButton. The only way to uncheck is to select some other RadioButton belonging to that RadioGroup.
RadioButton is a two states button which is either checked or unchecked. If a single radio button is unchecked, we can click it to make checked radio button. Once a radio button is checked, it cannot be marked as unchecked by user.
You can check the current state of a radio button programmatically by using isChecked() method. This method returns a Boolean value either true or false. if it is checked then returns true otherwise returns false. Below is an example code with explanation in which we checked the current state of a radio button.
Find your radio buttons in layout.xml and give them this:
android:button="@null"
This should do the same thing as presenter.setButtonDrawable(null);
Except that this actually works
Edit:
In case of code created button, please use:
presenter.setButtonDrawable(new StateListDrawable());
Actually helps as it is an equivalent of
android:button="@null"
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