Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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..

like image 835
enrm Avatar asked Oct 27 '25 06:10

enrm


2 Answers

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>
like image 123
Phantômaxx Avatar answered Oct 28 '25 21:10

Phantômaxx


You have here a great example of what you need:

Radio buttons Example

The layout:

<RadioGroup
    android:id="@+id/radioSex"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <RadioButton
        android:id="@+id/radioMale"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/radio_male"
        android:checked="true" />

    <RadioButton
        android:id="@+id/radioFemale"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/radio_female" />

</RadioGroup>

And for controling:

private RadioGroup radioSexGroup;
private RadioButton radioSexButton;
private Button btnDisplay;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    addListenerOnButton();
}

public void addListenerOnButton() {
    radioSexGroup = (RadioGroup) findViewById(R.id.radioSex);
    btnDisplay = (Button) findViewById(R.id.btnDisplay);

    btnDisplay.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // get selected radio button from radioGroup
            int selectedId = radioSexGroup.getCheckedRadioButtonId();
            // find the radiobutton by returned id
            radioSexButton = (RadioButton) findViewById(selectedId);

            Toast.makeText(MyAndroidAppActivity.this, radioSexButton.getText(), Toast.LENGTH_SHORT).show();
        }
    });
}
like image 30
Miguel Maciel Avatar answered Oct 28 '25 20:10

Miguel Maciel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!