5

I have a radio group with two radiobuttons. each button has a custom selector. I would like for the drawable in the selector to fill out the width of the radio button. Under different resolutions the button looks different. I am not using a nine-patch so this makes sense but even if i was I am not sure how to make the button drawable stretch. Below is the code for the radio group and a selector. widths are different from fiddling around.

The layout xml:

            <RadioButton
                android:id="@+id/radiomale"
                android:layout_width="126dp"
                android:layout_height="50dp"
                android:minHeight="50dp"
                android:minWidth="126dp"
                android:button="@layout/signupmale"

                />

            <RadioButton
                android:id="@+id/radiofemale"
                 android:layout_width="128dp"
                android:layout_height="50dp"
                android:minHeight="50dp"
                android:minWidth="128dp"
                android:button="@layout/signupfemale" />
        </RadioGroup>

The Selector:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:state_window_focused="false"
    android:drawable="@drawable/genre_male_pressed" />
<item android:state_checked="false" android:state_window_focused="false"
    android:drawable="@drawable/genre_male" />
<item android:state_checked="true" android:state_pressed="true"
    android:drawable="@drawable/genre_male_pressed" />
<item android:state_checked="false" android:state_pressed="true"
    android:drawable="@drawable/genre_male" />
<item android:state_checked="true" android:state_focused="true"
    android:drawable="@drawable/genre_male_pressed" />
<item android:state_checked="false" android:state_focused="true"
    android:drawable="@drawable/genre_male" />
<item android:state_checked="false" android:drawable="@drawable/genre_male" android:scaleType="fitXY" />
<item android:state_checked="true" android:drawable="@drawable/genre_male_pressed" android:scaleType="fitXY"/>
</selector>

The problem is when testing under different resolutions gaps form between the buttons. Thanks for any help.

Maurycy
  • 3,911
  • 8
  • 36
  • 44

1 Answers1

11

RadioGroup is actually a subclass of LinearLayout so what you could do is use layout_weight of the children to control the spacing. This might be a little helpful: Linear Layout and weight in Android and this Android layout_weight comportment

    <RadioGroup
        android:layout_width="match_parent"
        android:layout_height="45dip"
        android:weightSum="1"
        android:orientation="horizontal"
        >

        <RadioButton
            android:layout_width="match_parent"
            android:layout_height="40dip"
            android:layout_weight=".5"
            />

        <RadioButton
            android:layout_width="match_parent"
            android:layout_height="40dip"
            android:layout_weight=".5"
            />

    </RadioGroup>

You are going to need to use ninepatch and maybe use padding left or right if there is text on the radio buttons.

Community
  • 1
  • 1
sgarman
  • 6,152
  • 5
  • 40
  • 44
  • thank you for your response. I tried adding a layout weight and weight value for both the radiobutton and the nested button. No go :(. Also the weight sum was set as well. – Maurycy Nov 09 '11 at 20:55
  • 1
    Make sure you set the layout_width or height depending which way you want the buttons to stretch to fill_parent. – sgarman Nov 09 '11 at 22:05
  • Thanks i will use a nine patch on the button. Was trying to avoid doing this since parts of the button are not easily stretchable :(. Thanks again. – Maurycy Nov 10 '11 at 22:42
  • If you post the button I may be able to help you patch it so it works well, also maybe not :x – sgarman Nov 10 '11 at 22:43
  • Worked flawlessly for my radioGroup, Thank You – inVINCEable Jan 09 '15 at 13:35