Android dynamic RadioGroup/RadioButtons as flat buttons
Asked Answered
Q

1

7

this is a similar issue to what is mentioned in a related post but I thought it was different enough to get its own question. Here it goes:

I have been able to get the "radio circle" to disappear no problem when declaring the radio buttons in xml by setting the button attribute of the radio button to null like this:

<RadioButton
android:id="@+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="One"
android:background="@drawable/radio_button_selector"
android:button="@null"/>

But when i try and declare the radio buttons dynamically i cannot get the radio circle to disappear even when I do this:

myRadioButton.setButtonDrawable(null);

Here is my example and even though i set the button drawable to null the radio circle still appears.

RadioGroup myRadioGroup = (RadioGroup)findViewById(R.id.myRadioGroup);
for (int i = 0; i < arrayListOfRadioButtonStringNames.size(); i++)
{
    RadioButton myRadioButton = new RadioButton(this);
    myRadioButton.setText(arrayListOfRadioButtonStringNames.get(i));
    myRadioButton.setButtonDrawable(null);
    myRadioButton.setBackgroundResource(R.drawable.radio_button_selector);
    myRadioGroup.addView(myRadioButton);
}
myRadioGroup.invalidate();

if I instead set the button drawable to empty, like this:

myRadioButton.setButtonDrawable(android.R.id.empty);

the radio circle disappears, but the text doesn't go into the area where the radio circle should be. Here's some ascii art to show what it does:

setButtonDrawable(null): (O = radio circle)

-------------------
| O  One | O  Two |
-------------------

setButtonDrawable(android.R.id.empty):

-------------------
|    One |    Two |
-------------------

I have tried setting the text gravity, etc to get the text to go in that empty space, but it seems that the "radio circle" is still there but its just not visible.

Any help on my issue would be appreciated. Thanks.

Quelpart answered 28/4, 2011 at 16:9 Comment(2)
Actually setting the button drawable to android's empty resource worked... All i had to do was: myRadioButton.setButtonDrawable(android.R.id.empty); myRadioButton.setPadding(5,0,5,0); and then it worked (on the Galaxy Tab)... BUT: For some reason when I run it on the Xoom it crashes and gives me a "android.content.res.Resources$NotFoundException: File from drawable resource Id #_______" Why does the Xoom not have access to the android.R.id.empty resource, but the Galaxy Tab does?Quelpart
Fixed this problem. See post here: #5997859Quelpart
M
1

I saw that you already fixed this issue, but I'm just wondering if you ever tried using: setVisibility(View.GONE); I think that should work.

Marindamarinduque answered 30/5, 2011 at 7:23 Comment(2)
I haven't had a chance to try this out yet but wouldn't that just make the whole radio button disappear? I just want the 'dot' inside the radio button to be gone. Thanks for your suggestion though. :)Quelpart
Ah, yes, that's true. Missed that one :)Marindamarinduque

© 2022 - 2024 — McMap. All rights reserved.