Football Fans: Download the 2012 Schedule App from Google Play!


Go Back   Android Forums > Android Development > Application Development > Developer 101

Developer 101 101 Tutorials



Reply
 
LinkBack Thread Tools
Old December 24th, 2011, 03:08 PM   #1 (permalink)
Junior Member
 
ondrovic's Avatar
 
Join Date: Dec 2011
Location: Carmel, Indiana
Posts: 42
 
Device(s): Droid Charge
Thanks: 7
Thanked 1 Time in 1 Post
ondrovic@gmail.com
Thumbs down Spinner Selection change to different activity help

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

ondrovic is offline  
Reply With Quote
Sponsors
Old December 25th, 2011, 05:11 AM   #2 (permalink)
Member
 
Join Date: Aug 2010
Location: Norway
Posts: 308
 
Device(s): Samsung Galaxy Nexus
Thanks: 0
Thanked 52 Times in 51 Posts
Default

To start a new activity you call startActivity method like this:
Code:
Intent intent = new Intent(getApplicationContext(), FamilyPlansActivity.class);
startActivity(intent);
__________________
Android: Personal Storage, Lars Monsen Facts(Norwegian)
miXer is offline  
Reply With Quote
The Following User Says Thank You to miXer For This Useful Post:
ondrovic (December 25th, 2011)
Old December 25th, 2011, 09:23 AM   #3 (permalink)
Junior Member
 
ondrovic's Avatar
 
Join Date: Dec 2011
Location: Carmel, Indiana
Posts: 42
 
Device(s): Droid Charge
Thanks: 7
Thanked 1 Time in 1 Post
ondrovic@gmail.com
Default

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) {
            
        }
    }
ondrovic is offline  
Last edited by ondrovic; December 25th, 2011 at 11:52 AM.
Reply With Quote
Reply

Bookmarks


Go Back   Android Forums > Android Development > Application Development > Developer 101 User CP
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -5. The time now is 08:20 PM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
Custom vBulletin Skins by: Relivo