1

I need a custom radio button with a custom background, centered text, an icon immediately before the text, but without the default indicator.

Currently, I have the following code:

<RadioButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:button="@null"
    android:background="@drawable/bg_rbtn_custom"
    android:drawableLeft="@drawable/icon"
    android:text="Lorem ipsum…" />

The problem is the icon defined in drawableLeft, is pushed all the way to the left, i.e.

[icon]____Lorem ipsum…________

I need this:

____[icon]Lorem ipsum…________

The same thing happens when I use android:button="@drawable/icon; the icon is place at the left-most part of the View, and the text is then centred inside the left-over space (rather than being centred relative to the entire View). I am Android API 8, so I can't use drawableStart, so I need a way to duplicate its behaviour (at least I assume that's what it does). The text is dynamic, and will change at runtime, so I can't really hard code the padding.

My question is quite similar to this one, but that guy only needed a Button, but I need a RadioButton that'll work in a RadioGroup.

Community
  • 1
  • 1
cesar
  • 8,944
  • 12
  • 46
  • 59

1 Answers1

0

First thoughts would be to try

android:paddingLeft="#db"

where "#db" would be the number of pixels it would take to center everything


After trying your code and replacing the background and icon to local resources I had, I was unable to recreate the issue you are having. Could it be possible that the icon you are using has transparent pixels on its right edge?

Malo
  • 5
  • 6
  • which issue are you talking about? the `drawableStart` not appearing, or the image not appearing right next to the text? – cesar Aug 30 '12 at 06:44
  • I used both drawableStart and drawableLeft and both resulted in the icon showing up as well as the icon was flush next to the text. I was unsuccessful in creating the gap that you are experiencing between the text and icon and had no issues with drawableStart not appearing. Because of this I suggest switching icons and seeing if its a problem with the icon itself or is code related. – Malo Aug 30 '12 at 07:52
  • the reason I suggested paddingLeft was if you could get the two together, then you could "center" them by pushing them towards center since I believe android:gravity="center" will only center the text – Malo Aug 30 '12 at 08:04
  • `drawableLeft` works for me, but the only problem is the gap. The text is dynamic, so I can't hard code the padding. As for why `drawableStart` won't work, I just figured out it's cause I am using API 8, and `drawableStart` is API 14 haha. – cesar Aug 31 '12 at 03:03
  • Good to know about drawableStart! I wouldn't have ever thought of that. I am sorry I couldn't have been more help – Malo Aug 31 '12 at 03:58
  • It's all good. Do update if you figure it out. Also, thanks very much for your time. – cesar Sep 03 '12 at 11:16
  • You can't see the problem while using layout_width="wrap_content" because the view will shrink to the size of the text plus the drawableLeft. You need to use match_parent. Then the Button will stretch to a size larger then it's text and icon. The text will be centered and the icon will stick to the left border of the view. – marsbear Apr 24 '13 at 13:39