Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

button needs to fill parent radiobutton

Tags:

android

layout

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.

like image 632
Maurycy Avatar asked Nov 09 '11 20:11

Maurycy


1 Answers

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.

like image 61
sgarman Avatar answered Sep 27 '22 21:09

sgarman