Luis Mendo Luis Mendo - 1 month ago 7
Android Question

Hide radio button icon but not text

I need to hide a radio button's icon: something like setting it to invisible, but only the icon, not the text (

setInvisible
hides also the text). The icon should still take up space, so that the text is aligned with that of the other radio buttons. Also, the radio button (its text) should be clickable.

In other words, what I want is for the icon to be "transparent" (not visible), but otherwise "be there": be clickable, take up space.

I need to do this programmatically, not in XML.

Any ideas?

Answer

XML:

<RadioButton
   android:paddingLeft="31dp"
   android:button="@android:color/transparent" />

Java:

RadioButton myButton = (RadioButton) findViewById(R.id.radio);
   myButton.setButtonDrawable(android.R.color.transparent);
   myButton.setPadding(31, 0, 0, 0);

setPadding() takes int values that represent Padding in Pixels, see Definition@Google so adjust the Padding as required.

Comments