• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Spinner Selection change to different activity help

ondrovic

Newbie
So I am new to developing android apps and had a question. I have a spinner and based on the selection I was wondering the best way to call another class / layout.

Here is my main class
Code:
public class BestBuyMobileActivity extends Activity {
   
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        
      InitialSetup();
    }
    public void InitialSetup() {
        Spinner spinner = (Spinner) findViewById(R.id.planSelector);
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.plan_array, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);
        
        spinner.setOnItemSelectedListener(new PlanSelectionListener());
    }
    class PlanSelectionListener implements OnItemSelectedListener {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
            //Display Selected option
            
            Toast.makeText(parent.getContext(), parent.getItemAtPosition(pos).toString() + " selected ", Toast.LENGTH_LONG).show();
        }
        public void onNothingSelected(AdapterView<?> parent) {
            
        }
    }
}

So based on the selection from the spinner I want it to call another activity / go to another layout

I also have other classes defined FamilyPlansActivity and IndividualPlansActivity
Code:
public class FamilyPlansActivity extends Activity{
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.individual);
        
    }
}
Code:
public class IndividualPlansActivity extends Activity{
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.individual);
        
    }
}

So am I even on the right path? Any advice would be greatly appreciated

Thanks
 
To start a new activity you call startActivity method like this:
Code:
Intent intent = new Intent(getApplicationContext(), FamilyPlansActivity.class);
startActivity(intent);
 
Thanks will give that a shot.

That worked like a charm.. for anyone that is interested on how to change activities from a spinner without having a button here is the code. Still needs some work
Code:
class PlanSelectionListener implements OnItemSelectedListener {
        public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
            //Display Selected option
            if(parent.getItemAtPosition(pos).toString().equals("Individual Plans")) {
             Intent i = new Intent(getApplicationContext(),IndividualPlanActivity.class);
             startActivity(i);
            }
            Toast.makeText(parent.getContext(), parent.getItemAtPosition(pos).toString() + " selected ", Toast.LENGTH_LONG).show();
        }
        public void onNothingSelected(AdapterView<?> parent) {
            
        }
    }
 
Back
Top Bottom