Search code examples
androidradio-buttonvisibility

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?


Solution

  • 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.