javaandroidradio-button

Select gender in android like radioButton


I'm making a simple program where I want to be able to select my gender much like a radio-button behaviour, you either check female or male, and the other choice is supposed to be unchecked. I have separate images for the buttons, both pressed and not pressed.

I've stored the states in a selector file:

button_female.xml :

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:drawable="@drawable/gender_f"/>
<item android:state_pressed="true" android:drawable="@drawable/gender_f" />
<item android:drawable="@drawable/gender_f_notpressed" />
</selector>

And in layout/settings_activity.xml :

    <Button
        android:layout_width="130dp"
        android:layout_height="130dp"
        android:id="@+id/maleButton"
        android:layout_alignParentTop="true"
        android:layout_marginTop="80dp"
        android:layout_marginLeft="220dp"
        android:drawable="@drawable/button_male"
        android:onClick="onGenderButtonClicked"
        />

And in the activity, the onClick is written as

   public void onGenderButtonClicked(View view) {
        if(feButton.isPressed()){
            maButton.setEnabled(false);
            radioPressed = true;
        } else if (maButton.isPressed()){
            feButton.setEnabled(false);
            radioPressed = true;
        } else {
            radioPressed = false;
        }

    }

However, this doesn't work for several reasons..

1 : The pictures on the buttons don't show up, either pressed or unpressed.

2 : My code is bad, the buttons don't work as expected. I would like to have them behave as a radio group (you either choose male or female as your gender)..

So I seem to have two problems here. Does anyone know a good way to solve this? I'm very new to writing android apps..


Solution

  • If you need something like this:

    enter image description here

    You can use this layout:

    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context=".MainActivity"
        >
            <RadioGroup
                android:id="@+id/radioGrp"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:background="@drawable/gender_old"
                android:paddingTop="64dp"
                android:paddingLeft="8dp"
                android:paddingRight="8dp"
                >
                <RadioButton
                    android:id="@+id/radioM"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_gravity="left"
                    android:checked="true"
                    android:drawableRight="@drawable/male"
                    android:layout_weight="1"
                    android:textSize="14dp"
                    android:text="Male"
                />
                <RadioButton
                    android:id="@+id/radioF"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_gravity="right"
                    android:checked="false"
                    android:drawableRight="@drawable/female"
                    android:layout_weight="1"
                    android:textSize="14dp"
                    android:text="Female"
                />
            </RadioGroup>
    </RelativeLayout>