views:

20

answers:

1

Hi! I have a radiobuttongroup in a game app in Android. Before the user can go on to the actual game he has to choose a level, which are three radiobuttons. Now, if the user clicks "play" the app crashes. How can I use a validation to see if a button was chosen?

I have an edittext also, which I simply use a: if ((editText.getText().toString().equals(""))) to see if the user has written a name, but this doesn't work on radiobuttons, or at least my game crashes even when I try to use this type of check.

Any help appreciated!

A: 

You can take the RadioGroup id which contains 3 radio buttons.

  RadioGroup radiogrp = (RadioGroup) findViewById(R.id.RadioGroup);
  int id = radiogrp.getCheckedRadioButtonId();

This will return the identifier of the selected radio button in this group. Upon empty selection, the returned value is -1.

So you can put if condition and check whether the id == -1 than no radio button is selected.

krunal shah
Thanks! That worked perfectly.
Anna-Karin
@Anna-Karin - Please correct the answer if you found right.
krunal shah
Was that right? I make the check symbol green, and that is it?
Anna-Karin
@Anna-Karin - Ya this is right.
krunal shah