1. Download our Official Android App: Forums for Android!

Apps Spinner Selection change to different activity help

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

  1. ondrovic

    ondrovic Member
    Thread Starter
    Rank:
    None
    Points:
    36
    Posts:
    42
    Joined:
    Dec 24, 2011

    Dec 24, 2011
    42
    1
    36
    Carmel, Indiana
    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
     

    Advertisement

  2. miXer

    miXer Android Enthusiast
    Rank:
    None
    Points:
    53
    Posts:
    313
    Joined:
    Aug 12, 2010

    Aug 12, 2010
    313
    51
    53
    Norway
    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 Member
    Thread Starter
    Rank:
    None
    Points:
    36
    Posts:
    42
    Joined:
    Dec 24, 2011

    Dec 24, 2011
    42
    1
    36
    Carmel, Indiana
    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.  
     

Share This Page

Loading...