10

I made a custom RadioButton that looks as follow in a Android 5.0 device.

enter image description here

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.

enter image description here

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
}
wviana
  • 1,619
  • 2
  • 19
  • 47

5 Answers5

25

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"
Marijan
  • 711
  • 8
  • 16
  • It isn't a inflated layout. It's code created. I'll add it into the question. – wviana Jun 17 '16 at 13:33
  • 3
    In that case, try this: `presenter.setButtonDrawable(new StateListDrawable());` This is the equivalent of `android:button="@null"` – Marijan Jun 19 '16 at 14:52
  • 1
    Could you add it into your answer, Than I will accept it. Worked just as expected when using `new StateListDrawable()` instead of `null` – wviana Jun 20 '16 at 12:21
4

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" />
0

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.

Milack27
  • 1,619
  • 2
  • 20
  • 31
0

You can also try android:button="?android:selectableItemBackground"

From: https://stackoverflow.com/a/18537061/5093308

Zhou Hongbo
  • 1,297
  • 13
  • 25
-2

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);
Abhishek Kumar
  • 4,532
  • 5
  • 31
  • 53