Reputation: 1937
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
}
Upvotes: 11
Views: 8272
Reputation: 1505
You can also try android:button="?android:selectableItemBackground"
From: https://stackoverflow.com/a/18537061/5093308
Upvotes: 0
Reputation: 452
AppCompatRadioButton can hide the circle button for API<21, there is needed just declare the following:
<androidx.appcompat.widget.AppCompatRadioButton
android:id="@+id/radio_button"
android:button="@null"
app:buttonCompat="@null" />
Upvotes: 4
Reputation: 1689
Based on Marijan's answer, I created a WrappedRadioButton
class that overrides the setButtonDrawable
methods:
class WrappedRadioButton : RadioButton {
constructor(context: Context) : super(context)
constructor(context: Context, attrs: AttributeSet) : super(context, attrs)
constructor(
context: Context,
attrs: AttributeSet,
defStyleAttr: Int
) : super(context, attrs, defStyleAttr)
@RequiresApi(api = 21)
constructor(
context: Context,
attrs: AttributeSet,
defStyleAttr: Int,
defStyleRes: Int
) : super(context, attrs, defStyleAttr, defStyleRes)
override fun setButtonDrawable(resId: Int) {
super.setButtonDrawable(StateListDrawable())
}
override fun setButtonDrawable(drawable: Drawable?) {
super.setButtonDrawable(StateListDrawable())
}
}
So whenever I want a RadioButton with android:button="@null"
, I just use a WrappedRadioButton instead. There's no need to call setButtonDrawable
manually.
Upvotes: 0
Reputation: 4808
setting your custom drawable to radiobutton is staright-forward, you can write something like this programmatically,
radioButton.setButtonDrawable(null);
radioButton.setBackgroundResource(R.drawable.your_custom_drawable);
Upvotes: -2
Reputation: 731
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"
Upvotes: 27