Opening a certain Activity depending on which Radio Button is selected?
-
28-05-2021 - |
Question
I've got a RadioGroup with two buttons in it.
I have a "Create" button that when pressed, starts one of two activities. If the first RadioButton, search, is checked when the "Create" Button is pressed, it should open the "ProductSearch" activity.
If the second RadioButton, type, is checked when the "Create" Button is pressed, it should open the "TypeEntries" activity. For now, though, it opens "ProductSearch" no matter which RadioButton is pressed.
So it works for search but not type.
RadioButton search = (RadioButton) findViewById(R.id.radio1);
RadioButton type = (RadioButton) findViewById(R.id.radio2);
Button create = (Button) findViewById(R.id.create);
if(search.isChecked()){
create.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent createIntent = new Intent(getApplicationContext(), ProductSearch.class); // <----- START "SEARCH" ACTIVITY
startActivityForResult(createIntent, 0);
}
});
}else if(type.isChecked()){
create.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent typeIntent = new Intent(getApplicationContext(), TypeEntries.class); // <----- START "TYPE ENTRIES OUT" ACTIVITY
startActivityForResult(typeIntent, 0);
}
});
}
}
}
Solution
try this :
RadioButton search = (RadioButton) findViewById(R.id.radio1);
RadioButton type = (RadioButton) findViewById(R.id.radio2);
Button create = (Button) findViewById(R.id.create);
create.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(search.isChecked()){
Intent createIntent = new Intent(getApplicationContext(), ProductSearch.class); // <----- START "SEARCH" ACTIVITY
startActivityForResult(createIntent, 0);
}
else
{
if(type.isChecked())
{
Intent typeIntent = new Intent(getApplicationContext(), TypeEntries.class); // <----- START "TYPE ENTRIES OUT" ACTIVITY
startActivityForResult(typeIntent, 0);
}
}
}
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow