0

I have two radio buttons where it calls same spinner arrays. So based on radio buttons how to call two activities. My code are as follows: in this i use two radio buttons,spinner and a button in xml

A.java

 Spinner selectcity; RadioGroup rg = null; RadioButton rb1,rb2; Button searchbutton; rg = (RadioGroup) findViewById(R.id.radioGroup); rb1=(RadioButton)findViewById(R.id.seekers); rb2=(RadioButton)findViewById(R.id.posting); selectcity = (Spinner) findViewById(R.id.statespinners); rb1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub selectcity=(Spinner)findViewById(R.id.statespinners); ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource( SearchCity.this, R.array.cities, android.R.layout.simple_spinner_item); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); selectcity.setAdapter(adapter); } }); rb2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub selectcity=(Spinner)findViewById(R.id.statespinners); ArrayAdapter<CharSequence> adapter1 = ArrayAdapter.createFromResource( SearchCity.this, R.array.area, android.R.layout.simple_spinner_item); adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); selectcity.setAdapter(adapter1); } }); searchbutton=(Button)findViewById(R.id.statebutton); searchbutton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //here i am strucked how to call two activities based on radio buttons selection } }); 

How use two radio button change and to call two different activities. (A and B).Thanks in advance.

2
  • What do you want, you mean if one radio buttion is selected you have to goto activity A otherwise activity B??? Commented Oct 24, 2013 at 7:19
  • You asked "How to open two activities". I think you mean "How to open one of the two activities". isn't it? Commented Oct 24, 2013 at 7:29

3 Answers 3

1

I think you are struggled with this. You could have a reference to the radio group and use getCheckedRadioButtonId () to get the checked radio button id.

int id = radioGroup.getCheckedRadioButtonId(); if (id == -1){ //no item selected } else{ if (id == R.id.seekers){ //call activity A } if (id == R.id.posting){ //call activity B } } 
Sign up to request clarification or add additional context in comments.

Comments

0
 Do like this. searchbutton=(Button)findViewById(R.id.statebutton); searchbutton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if(rb1.isChecked()){ //radio button rb1 // TODO Auto-generated method stub Intent intent=new Intent(this,ActivityA.class); startActivity(intent); } else { if(rb2.isChecked())//radio button rb2 { Intent intent=new Intent(this,ActivityB.class); startActivity(intent); } } } }); 

Comments

0
@Override public void onCheckedChanged(RadioGroup group, int checkedId){ if(checkedId == R.id.seekers) { // Call First Activity }else if(checkedId == R.id.posting) { // Call Second Activity } } 

Comments