How can I programmatically set android:button="@null"?
Asked Answered
H

5

24

I'm trying to programmatically add a set of RadioButtons to a RadioGroup like so:

for (int i = 0; i < RANGE; i++) {
    RadioButton button = new RadioButton(this);
    button.setId(i);
    button.setText(Integer.toString(i));
    button.setChecked(i == currentHours); // Select button with same index as currently selected number of hours
    button.setButtonDrawable(Color.TRANSPARENT);
    button.setBackgroundResource(R.drawable.selector);
    button.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View view) {
            ((RadioGroup)view.getParent()).check(view.getId());
            currentHours = view.getId();
        }
    });

    radioGroupChild.addView(button);
}

and I need to set the button drawable to null (I want just text on top of my background). Manually doing this in the XML with android:button="@null" works great, but I don't want to hardcode each radio button. I've tried just doing button.setButtonDrawable(null) but it doesn't change anything.

Any help is greatly appreciated!

Haulage answered 29/4, 2013 at 17:12 Comment(1)
This is a better answer in case you might actually want to use a background selector https://mcmap.net/q/581895/-how-can-i-make-a-radiobutton-with-overlapping-textSettling
Q
9

You should do this:

button.setBackgroundDrawable(null);

If your drawable has a reference to selector you can make it transparent through your selector xml:

<item android:drawable="@android:color/transparent" />

as you'd probably found the solution ;)

Quarrel answered 29/4, 2013 at 17:17 Comment(6)
I said above, I've tried this and it doesn't work for some reason.Haulage
It's setBackgroundDrawable, not setButtonDrawable.Quarrel
Oh, sorry, didn't see that. The problem is, that just removes the background, which where I'm setting my custom selector drawable.Haulage
Ok, then you should set a transparent color in your selector xml.Quarrel
Awesome, it worked with my selector with just this item: <item android:drawable="@android:color/transparent" />. If you add that to your answer, I'll mark it! Thanks!Haulage
As of API 16, setBackgroundDrawable is deprecated. Use setBackground(null) instead in this case.Commensurate
C
47

You need to set an empty StateListDrawable as the drawable. So the Java equivalent of android:button="@null" is:

radioButton.setButtonDrawable(new StateListDrawable());
Chequered answered 18/9, 2013 at 0:26 Comment(4)
This worked perfectly for me. Not sure why it is not the best answer.Commensurate
this is same as setButtonDrawable(android.R.color.transparent);Maren
Not on all devices, why? did not work on hTC one X, and worked on Nexus 4.Propagandist
This worked better than the answer that is set as the answer! Thank youDday
Q
9

You should do this:

button.setBackgroundDrawable(null);

If your drawable has a reference to selector you can make it transparent through your selector xml:

<item android:drawable="@android:color/transparent" />

as you'd probably found the solution ;)

Quarrel answered 29/4, 2013 at 17:17 Comment(6)
I said above, I've tried this and it doesn't work for some reason.Haulage
It's setBackgroundDrawable, not setButtonDrawable.Quarrel
Oh, sorry, didn't see that. The problem is, that just removes the background, which where I'm setting my custom selector drawable.Haulage
Ok, then you should set a transparent color in your selector xml.Quarrel
Awesome, it worked with my selector with just this item: <item android:drawable="@android:color/transparent" />. If you add that to your answer, I'll mark it! Thanks!Haulage
As of API 16, setBackgroundDrawable is deprecated. Use setBackground(null) instead in this case.Commensurate
A
6

setButtonDrawable(getResources().getDrawable(android.R.color.transparent)) should do the trick.

Only this worked for me.

Aga answered 3/12, 2014 at 9:58 Comment(1)
You can simply put setButtonDrawable(android.R.color.transparent); as @amalan pointed out in a previous comment.Drawbar
H
4

What worked for me in multiple devices and APIs and is as simple as it gets was setButtonDrawable(android.R.color.transparent);

Hack answered 5/9, 2015 at 0:11 Comment(0)
C
0

in Kotlin you can do that

radioButton.buttonDrawable = null
Crossrefer answered 10/1, 2023 at 8:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.