Newbee to Android development.
I am developing a application, which have three different tabs. I want to have common menu options, like About and Refresh, on all tabs and in each individual need the specific menu accordingly.
MyAPP Main():
public class MyApp extends TabActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Resources res = getResources(); // Resource object to get Drawables
TabHost tabHost = getTabHost(); // The activity TabHost
TabHost.TabSpec spec; // Resusable TabSpec for each tab
Intent intent; // Reusable Intent for each tab
// Create an Intent to launch an Activity for the tab (to be reused)
intent = new Intent().setClass(this, Bundles.class);
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("bundles").setIndicator("Bundles",
res.getDrawable(R.drawable.ic_tab_bundle_grey))
.setContent(intent);
tabHost.addTab(spec);
// Do the same for the other tabs
intent = new Intent().setClass(this, Policies.class);
spec = tabHost.newTabSpec("policies").setIndicator("Policies",
res.getDrawable(R.drawable.ic_tab_policy_grey))
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, Inventory.class);
spec = tabHost.newTabSpec("Inventory").setIndicator("Inventory",
res.getDrawable(R.drawable.ic_tab_settings_grey))
.setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(2);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.layout.menu, menu);
return true;
}
public void onClick(View v) {
switch(v.getId()) {
case R.id.about:
Intent i = new Intent(this, About.class);
startActivity(i);
break;
case R.id.refresh:
Intent j = new Intent(this, RefreshAgent.class);
startActivity(j);
break;
// more buttons to go here later
}
}
In other Inventory tab:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.layout.settings_menu, menu);
return true;
}
public void onClick(View v) {
switch(v.getId()) {
case R.id.register:
Intent i = new Intent(this, About.class);
startActivity(i);
break;
case R.id.reregister:
Intent j = new Intent(this, Reregister.class);
startActivity(j);
break;
case R.id.accountsettings:
Toast.makeText(this, "Account Settings", Toast.LENGTH_LONG).show();
break;
}
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.register:
Toast.makeText(this, "Registration Not implemented!", Toast.LENGTH_LONG).show();
return true;
case R.id.reregister:
Toast.makeText(this, "Re Registration Not implemented!", Toast.LENGTH_LONG).show();
//TODO : Use refresh agent class
//startActivity(new Intent(this, RefreshAgent.class));
return true;
case R.id.accountsettings:
//Intent j = new Intent(this, RefreshAgent.class);
//startActivity(j);
Toast.makeText(this, "Account Settings", Toast.LENGTH_LONG).show();
break;
}
return false;
Above implementation displays About and Refresh on other two tabs, but on Inventory tab it displays only two which are defined in Inventory tab.
Thanks in advance for the help.
I didn’t know if your two code sample are in the same class (i never used tabactivity) but i will anwser in both case.
In the first code sample, you do
It is this line who show the about and refresh menu.
In your inventory tab, you inflate R.layout.settings_menu , which do not contain about and refresh, that is why you don’t have common menu in all your tab.
You need to inflate, in all your tab R.layout.menu (which i advise you to rename common_menu).
By example, on inventory tab, you just need to do that :
Menu are inflated in that order, so you will see common menu on bottom, and the specific menu on the top.
If you are motivated, you can probably do a “base activity” who would inflate on onCreateOptionMenu the common menu, and in your specific activity (who would extend your base activity), you could call super.onCreateOptionMenu(), and inflate what you need after.
You need to inflate another menu in function of the current tab showed. There is one problem tough, the onCreateOptionMenu is only called once by activity, so if you change the tab and press menu, it will just show the precedent menu.
Fortunatelly, there is another method that you can use too. It’s onPrepareOptionMenu(). This method is called every time you show the menu in your application. So, you just need to inflate your specific menu in here.
You can read this link : http://developer.android.com/guide/topics/ui/menus.html
But basically, you just have to do that :
And keep your first onCreateOptionMenu, who will inflate the common menu each time.