Android radiobutton background style when selected
Asked Answered
H

3

18

I know how to set background for my radio button. But I don't know what is the best/right way to change background of selected radio button? Is it possible to make it in xml or has to be done in code?

Regards

Halloween answered 5/6, 2011 at 19:58 Comment(0)
P
44

Just create a selector xml file in drawable folder

checkbox_background.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
      <item android:state_checked="true" android:drawable="@color/checkbox_active" />
      <item android:state_checked="false" android:drawable="@color/checkbox_inactive" />
</selector>

And apply this background to your radio buttons

 <RadioButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/checkbox_background" />
Pretentious answered 12/11, 2012 at 14:49 Comment(2)
i wanna a to change background in radiobutton a circle .Acarus
it fills complete background of radio buttonSamarium
P
1

You can define a selector for your radio button as described in this answer:

Is it possible to change the radio button icon in an android radio button group

Pascale answered 5/6, 2011 at 20:10 Comment(3)
I have defined two selectors: one for background and one for button. The problem now is that text inside radio button does not start at the end of button but at start of radio button. In other words it is aligned left based on whole radio button.Halloween
@Halloween having same same problem did you find solution.Percipient
I had to add padding, like this: radioChoice.setPadding( (radioChoice.getPaddingLeft() + 3), 0, 3, 1 );Effervescent
C
-12

Use

android:gravity="center"

for the radio button. This will center the text.

Crackpot answered 24/10, 2014 at 14:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.