0

I am trying to check if at least a radio button is checked inside a radiogroup. Obviously i can choose to check each one at a time, But according to Android Developers Reference Site, public int getCheckedRadioButtonId () is a public method of radiogroup which returns -1 upon empty election. so this is my codes

if(radioGroup.getCheckedRadioButtonId() == -1){
        Toast.makeText(getApplicationContext(),"Please make a selection",Toast.LENGTH_SHORT);
        return;
    }
 <RadioGroup
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/radioGroup"
        android:layout_below="@id/loanPurposeTV">

        <RadioButton
            android:id="@+id/radioButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            android:text="Personal"
            android:checked="false" />

        <RadioButton
            android:id="@+id/radioButton2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_toEndOf="@+id/radioButton"
            android:layout_toRightOf="@+id/radioButton"
            android:text="Bussiness" />

        <RadioButton
            android:id="@+id/radioButton3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/radioButton2"
            android:layout_toEndOf="@+id/radioButton2"
            android:layout_toRightOf="@+id/radioButton2"
            android:text="Option Three" />

        <RadioButton
            android:id="@+id/radioButton4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_toEndOf="@+id/radioButton3"
            android:layout_toRightOf="@+id/radioButton3"
            android:text="Option Four" />

        <RadioButton
            android:id="@+id/radioButton5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_toEndOf="@+id/radioButton4"
            android:layout_toRightOf="@+id/radioButton4"
            android:text="Other" />
    </RadioGroup>

But its not working as intended. Does anybody know why its not working or a much simpler way to implement my idea

stebak
  • 165
  • 1
  • 8
  • 1
    What specifically isn't working? Is it always -1? – em_ Oct 21 '15 at 13:49
  • 1
    "But its not working as intended" -- please explain your exact symptoms. Please also show how you are setting up the `RadioButtons` and the `RadioGroup`. – CommonsWare Oct 21 '15 at 13:50
  • @ElliotM if i dont select any of the radiobuttons in the radio group, the toast is supposed to show that message which is not working. – stebak Oct 21 '15 at 13:57
  • We will need to see the code where you set up the radio group. – em_ Oct 21 '15 at 13:58
  • @ElliotM i jst updated my question with the xml codes – stebak Oct 21 '15 at 14:02

1 Answers1

1

First of all, you forgot ".show()" when you call your Toast.

Personnally, I encountered the same problem (it returned always -1, even when a radiobutton is selected), in spite of there are answers that prove it works, like this one

You can try them, but honnestly, when I read this, I tried everything but I never made it work. Maybe I was too in a hurry when I had this problem.

Otherwise, a very simple workaround would be to set selected a default radiobutton when the layout loads, so, you're sure there will be one radiobutton well selected (and need no longer check before validation)

Or you can create a bool var initialized to false which will be set to true as soon as any radiobutton is selected (use a OnCheckChangeListener).

Community
  • 1
  • 1
PAD
  • 2,240
  • 1
  • 22
  • 28
  • 1
    sometimes you just think too much that you end up omitting very little things, thanks man. didn't realize i wasn't showing the toast. – stebak Oct 21 '15 at 14:16
  • I understand @KBJ, it even happens to the best :) – PAD Oct 21 '15 at 14:18
  • when using onCheckChangeListener, can you get the string of the radio button with their ids? or I have to do that the hard way. – stebak Oct 21 '15 at 14:37