Android: help with RadioButton Toast

enter image description here

If the user clicks the “NEXT” button without selecting an option, he must fry the message “pls select any one”, otherwise he must go to the next screen. I tried but am not going to go to the next screen. Instead, it shows a “pls select any one” toast and my code

public class Question1 extends Activity implements OnClickListener { Button vid, next; Typeface tp; TextView tv; RadioGroup rg; RadioButton rb; Button b; SharedPreferences sp; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.question1); sp = getSharedPreferences("AppFile1", 0); tv = (TextView) findViewById(R.id.tvQuestion1); rg = (RadioGroup) findViewById(R.id.radioGroup1); vid = (Button) findViewById(R.id.buttonQuestion1VIdeo); next = (Button) findViewById(R.id.buttonQuestion1Next); vid.setOnClickListener(this); next.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.buttonQuestion1VIdeo: Intent i = new Intent(Question1.this, VideoActivity.class); startActivity(i); break; case R.id.buttonQuestion1Next: if (next.isSelected()) { int selectedId = rg.getCheckedRadioButtonId(); rb = (RadioButton) findViewById(selectedId); String s = rb.getText().toString(); SharedPreferences.Editor ed = sp.edit(); ed.putString("q1", s); ed.commit(); Intent ia = new Intent(Question1.this, Question2.class); startActivity(ia); } else { Toast.makeText(getApplicationContext(), "Please Select Answer", 0).show(); } break; default: break; } } 

}

+4
source share
4 answers

Try:

 if (rg.getCheckedRadioButtonId()!=-1) { int selectedId = rg.getCheckedRadioButtonId(); rb = (RadioButton) findViewById(selectedId); String s = rb.getText().toString(); SharedPreferences.Editor ed = sp.edit(); ed.putString("q1", s); ed.commit(); Intent ia = new Intent(Question1.this, Question2.class); startActivity(ia); } else { Toast.makeText(getApplicationContext(), "Please Select Answer", 0).show(); } 

The documentation states that if the button is not checked , getCheckedRadioButtonId () returns -1 .

Returns the identifier of the selected switch in this group. If empty, the return value is -1.

Therefore, if you have any value other than -1, then you have the selected button.

+4
source

wrong condition : if (next.isSelected())

True condition: How do you want to make sure that the option must be selected:

 boolean checked = ((RadioButton) view).isChecked(); 
+3
source

You check the selection state of the Next button itself in the state. But you want to check the switch selection status. Therefore, you should specify it as:

 if (rg.getCheckedRadioButtonId()!=-1) { int selectedId = rg.getCheckedRadioButtonId(); rb = (RadioButton) findViewById(selectedId); String s = rb.getText().toString(); SharedPreferences.Editor ed = sp.edit(); ed.putString("q1", s); ed.commit(); Intent ia = new Intent(Question1.this, Question2.class); startActivity(ia); } else { Toast.makeText(getApplicationContext(), "Please Select Answer", 0).show(); } 
+3
source

the condition will be as

 rb1 = (RadioButton)findViewById(R.id.rb1); rb2 = (RadioButton)findViewById(R.id.rb2); rb3 = (RadioButton)findViewById(R.id.rb3); if(rb1.isChecked()==false && rb2.isChecked()==false && rb3.isChecked()==false) { // Toast message is here } else { // Intent code is here } 
0
source

All Articles