Geeks With Blogs
Bill Osuch - Random geek notes

Say you've got an app that has multiple activities, and you want each activity to use the same options menu (the menu that appears when the user taps the MENU button). Rather than repeating the Inflater and ItemSelected code on each activity, just create a new activity class and have all your other activities extend it rather than Activity.

To start, create an xml layout for your menu (in the res/menu directory):

<menu xmlns:android="http://schemas.android.com/apk/res/android">
   <item android:id="@+id/new"
      android:icon="@drawable/new"
      android:title="New" />
   <item android:id="@+id/about"
      android:icon="@drawable/about"
      android:title="About" />
</menu>

Here I've got just two items, but you can use as many as you'd like. Be sure to drop your icon files into the res/drawable directory.

Then, create your new Activity that all other activites will extend:

public class CustomMenuActivity extends Activity {

@Override
public boolean onCreateOptionsMenu(Menu menu) {  
   MenuInflater inflater = getMenuInflater();
   inflater.inflate(R.menu.menu, menu);
   return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {  
   switch (item.getItemId()) {
   case R.id.about:
      showAbout();
      return true;
   case R.id.new:
      doNew();
      return true;
   }
     
   return true;
}

private void showAbout(){
   //Show the about screen
}

private void doNew(){
   //Do something new
}

}

Finally, instead of having all your other classes extend Activity, have them extend CustomMenuActivity:

public class MyClass extends CustomMenuActivity {
 

Technorati Tags: Android

Posted on Friday, July 15, 2011 9:55 AM Android | Back to top


Comments on this post: Android: Sharing an options menu between mutiple activities tutorial

# re: Android: Sharing an options menu between mutiple activities tutorial
Requesting Gravatar...
I too have done this recently and it is a great way to use global menus. Another thing you might add is that you can override the onCreateOptionsMethod and as long as you declare the super(super.onCreateOptionsMenu(menu) you will be able to add menu items to an activity while still keeping the global menu. You will also have to override the onOptionsItemSelected method to deal with the added menu items. I can produce a follow up to this concept to demonstrate on my blog if you like.
Left by Roger O'Dell on Jul 15, 2011 10:42 AM

# re: Android: Sharing an options menu between mutiple activities tutorial
Requesting Gravatar...
Hi,

Thanks for the post.
Its really helpful for me.

Great job.....!
Left by Pravin on Oct 12, 2011 3:11 AM

# re: Android: Sharing an options menu between mutiple activities tutorial
Requesting Gravatar...
Thank you
http://geekswithblogs.net

Your post is so usefull to me to make my tesist..
Left by mona_droid on Mar 31, 2012 3:09 AM

# re: Android: Sharing an options menu between mutiple activities tutorial
Requesting Gravatar...
Good idea, thanks for sharing it
Left by Matt on Nov 06, 2012 6:01 PM

# re: Android: Sharing an options menu between mutiple activities tutorial
Requesting Gravatar...
Its great way to use menu globally. But i am having an issue that I have activity which already extents FragmentActivity........... :(
What to do to solve this problem
Your help will be greatfull to me
Left by Yash on Jun 04, 2014 1:00 AM

# Thank you
Requesting Gravatar...
Thank you for sharing. It saved me a lot of time.

I am really grateful to you!
Left by Surya Teja on Sep 19, 2014 10:07 AM

Your comment:
 (will show your gravatar)


Copyright © Bill Osuch | Powered by: GeeksWithBlogs.net