Hide radio button icon but not text
Asked Answered
A

5

23

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?

Accelerator answered 28/8, 2013 at 18:58 Comment(2)
You could create a RadioButton with no Text, and to the left of it a TextView containing the RadioButtons description.Tical
@PhilippJahoda Good idea, but in my case it is difficult to apply, because I need to turn the button from "normal" to "no icon" and backAccelerator
S
49

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.

Scourings answered 28/8, 2013 at 19:16 Comment(5)
@MBennett Thanks. It works, except that "31dp" (a string) is not allowed as a first argument to setPadding. I have changed it to (int) (31*getResources().getDisplayMetrics().density + 0.5f, so as to convert from 31dp to pixels. Maybe you want to change your answer accordingly. It turns out to be too narrow, maybe I'll have to play with the width. Or have I done the conversion wrong?Accelerator
34dp (converted to pixels) seems to work in Android 4.0. In 2.3 a slightly larger value is requiredAccelerator
This works for me myButton.setButtonDrawable(android.R.color.transparent); where myButton.setButtonDrawable(null); did not thanks.Sapir
when I use 'android:button="@android:color/transparent"' ,while selecting radiobutton also ,selection is not visibleUndercroft
radio button circle should not be visible, at the same time, when I select tick mark should come -How to achieve this, any helpUndercroft
E
13

set android:button="@null" will remove the default radio icon

<RadioButton
     android:id="@+id/rb1"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:button="@null"
     android:text="Female" />
Emilie answered 22/10, 2016 at 19:24 Comment(0)
K
8

Old post, but it might help someone. Setting android:button="@null" didn't worked for me for older versions. The only way that I could hide the circle was setting this style to the RadioButton

<style name="Radio" parent="Widget.AppCompat.CompoundButton.RadioButton">
  <item name="buttonCompat">@null</item> <!-- Key! -->
  <item name="android:button">@null</item>
</style>
Kob answered 23/2, 2020 at 16:0 Comment(0)
L
2

In order to hide the circle button, there is needed just declare the following

<RadioButton
  app:buttonCompat="@null"
  android:button="@android:color/transparent" />
Litigation answered 2/8, 2020 at 18:40 Comment(0)
G
1

For hiding default circle icon in radio button just add a single line:

android:button="@null"
Gwenore answered 9/8, 2021 at 3:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.