Reputation: 2899
I'm writing an application which I want to be able to launch a second class with a different layout when a menu button is pressed. the code I have to switch classes is:
case Menubutton1:
Intent i = new Intent(Budgeter.this, Outgoings.class);
startActivity(i);
return true;
(Obviously within a case statement)
How do I create an xml file which only relates to the second class? Also do I need to edit AndroidManifest.xml?
Finally if anyone could point me towards some good tutorials on intents I would greatly appreciate it.
Upvotes: 1
Views: 158
Reputation: 48871
I'm guessing here that you're confused about thinking the layout file for an Activity has to be called main.xml??? If so, this is not the case...
You can have budgeter.xml
, outgoings.xml
etc etc.
Just use setContentView(R.layout.budgeter)
in the Budgeter Activity's onCreate(...)
method and setContentView(R.layout.outgoings)
in the Outgoings Activity onCreate(...)
and so on.
Also do I need to edit AndroidManifest.xml?
Yes, all Activities must be registered in AndroidManifest.xml
As for working with Intents, try this as a starter...
Upvotes: 0
Reputation: 34026
in first class write a method
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuItem mnuHome =menu.add(0, 0, 0, "Home");
mnuHome.setAlphabeticShortcut('h');
mnuHome.setIcon(R.drawable.home_icon);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getTitle() == "Home") {
Intent i = new Intent(Budgeter.this, Outgoings.class);
startActivity(i);
finish();
}
}
And in second class you have to override onCreate() method and there you can set Layout as
setContentView(R.Layout.XML);
Also for second class you have to define in menifest.xml
Upvotes: 1
Reputation: 780
you should add the second(any) class in AndroidManifest.xml file if the class extended from android core components(Activity,Service,ContentProvider,BroadcastReceiver,BroadcastReceiver is a little different).It is possible to create another XML file in "layout" under "res".
Upvotes: 0