1. Are you ready for the Galaxy S20? Here is everything we know so far!

Spinner Selection change to different activity help

Discussion in 'Android Development' started by ondrovic, Dec 24, 2011.

  1. ondrovic

    ondrovic Newbie
    Thread Starter

    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 (Text):
    1.  
    2. public class BestBuyMobileActivity extends Activity {
    3.    
    4.     /** Called when the activity is first created. */
    5.     @Override
    6.     public void onCreate(Bundle savedInstanceState) {
    7.         super.onCreate(savedInstanceState);
    8.         setContentView(R.layout.main);
    9.        
    10.        
    11.       InitialSetup();
    12.     }
    13.     public void InitialSetup() {
    14.         Spinner spinner = (Spinner) findViewById(R.id.planSelector);
    15.         ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.plan_array, android.R.layout.simple_spinner_item);
    16.         adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    17.         spinner.setAdapter(adapter);
    18.        
    19.         spinner.setOnItemSelectedListener(new PlanSelectionListener());
    20.     }
    21.     class PlanSelectionListener implements OnItemSelectedListener {
    22.         @Override
    23.         public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
    24.             //Display Selected option
    25.            
    26.             Toast.makeText(parent.getContext(), parent.getItemAtPosition(pos).toString() + " selected ", Toast.LENGTH_LONG).show();
    27.         }
    28.         public void onNothingSelected(AdapterView<?> parent) {
    29.            
    30.         }
    31.     }
    32. }
    33.  
    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 (Text):
    1.  
    2. public class FamilyPlansActivity extends Activity{
    3.     public void onCreate(Bundle savedInstanceState) {
    4.         super.onCreate(savedInstanceState);
    5.         setContentView(R.layout.individual);
    6.        
    7.     }
    8. }
    9.  
    Code (Text):
    1.  
    2. public class IndividualPlansActivity extends Activity{
    3.     public void onCreate(Bundle savedInstanceState) {
    4.         super.onCreate(savedInstanceState);
    5.         setContentView(R.layout.individual);
    6.        
    7.     }
    8. }
    9.  
    So am I even on the right path? Any advice would be greatly appreciated

    Thanks
     



    1. Download the Forums for Android™ app!


      Download

       
  2. miXer

    miXer Android Enthusiast

    To start a new activity you call startActivity method like this:
    Code (Text):
    1. Intent intent = new Intent(getApplicationContext(), FamilyPlansActivity.class);
    2. startActivity(intent);
     
    ondrovic likes this.
  3. ondrovic

    ondrovic Newbie
    Thread Starter

    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 (Text):
    1.  
    2. class PlanSelectionListener implements OnItemSelectedListener {
    3.         public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
    4.             //Display Selected option
    5.             if(parent.getItemAtPosition(pos).toString().equals("Individual Plans")) {
    6.              Intent i = new Intent(getApplicationContext(),IndividualPlanActivity.class);
    7.              startActivity(i);
    8.             }
    9.             Toast.makeText(parent.getContext(), parent.getItemAtPosition(pos).toString() + " selected ", Toast.LENGTH_LONG).show();
    10.         }
    11.         public void onNothingSelected(AdapterView<?> parent) {
    12.            
    13.         }
    14.     }
    15.  
     
Loading...
Similar Threads - Spinner Selection change
  1. sachertorte
    Replies:
    1
    Views:
    424
  2. Ma1_funti0n
    Replies:
    5
    Views:
    1,072
  3. InfernalByte
    Replies:
    6
    Views:
    3,349
  4. MPhil
    Replies:
    1
    Views:
    943
  5. JamesUK
    Replies:
    2
    Views:
    1,540
  6. Arron Mothersole
    Replies:
    3
    Views:
    1,519
  7. Dacdariel
    Replies:
    0
    Views:
    882
  8. James_Watson
    Replies:
    3
    Views:
    5,215
  9. Adi Inbar
    Replies:
    2
    Views:
    1,577
  10. Clemworld
    Replies:
    2
    Views:
    4,917

Share This Page

Loading...