Customizing Background and Text color in Options Menu Android

April 19, 2011 - Uncategorized

Options menu in android applications can be customized to set the background or change the text appearance. The background and text color in the menu couldn’t be changed using themes and styles. The android source code (datareslayouticon_menu_item_layout.xml)uses a custom item of class “com.android.internal.view.menu.IconMenuItem”View for the menu layout. We can make changes in the above class to customize the menu. To achieve the same, use LayoutInflater factory class and set the background and text color for the view.

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

getLayoutInflater().setFactory(new Factory() {
@Override
public View onCreateView(String name, Context context,
AttributeSet attrs) {

if (name .equalsIgnoreCase(“com.android.internal.view.menu.IconMenuItemView”)) {
try {
LayoutInflater f = getLayoutInflater();
final View view = f.createView(name, null, attrs);

new Handler().post(new Runnable() {
public void run() {

// set the background drawable
view .setBackgroundResource(R.drawable.my_ac_menu_background);

// set the text color
((TextView) view).setTextColor(Color.WHITE);
}
});
return view;
} catch (InflateException e) {
} catch (ClassNotFoundException e) {
}
}
return null;
}
});
return super.onCreateOptionsMenu(menu);
}

› tags: android /

Comments

  1. Great post, Thanks for sharing the code for Customizing Background and Text color in Options Menu.

  2. jetti Madhu says:

    Hi,
    Thanks for posting the menu Item concept.

    final View view = f.createView(name, null, attrs);
    When I declare the view as global instead of local, I able to apply the resource to the last Item of the menu.Not applying to all the menu items.
    Is any thing wrong with me?

  3. bo says:

    Didn’t work for me. Always end up with java.lang.IllegalStateException: A factory has already been set on this LayoutInflater

  4. Ali says:

    It doesn’t work… Always end with exception…

  5. udit says:

    doesn’t work

  6. Andrew says:

    I don’t know why people keep referencing this article, its clearly not working and leaves far to many unanswered questions.

  7. sanjeet ajnabee says:

    hey, it doesn’t change the inflater menu background , only changes the items background.

  8. Jyothi says:

    instead of LayoutInflater f = getLayoutInflater(); use LayoutInflater li = LayoutInflater.from(context); It will work perfect.

  9. media stream says:

    I want to show my appreciation for your generosity supporting persons that actually need help with the theme. Your special commitment to passing the message up and down came to be extremely significant and has in most cases empowered many people like me to arrive at their targets. Your entire helpful suggestions signifies much a person like me and additionally to my colleagues. Best wishes; from everyone of us.

  10. It is the best time to make some plans for the future and it’s time to be happy. I have read this post and if I could I wish to suggest you some interesting things or tips. Maybe you can write next articles referring to this article. I want to read even more things about it!

  11. I’m extremely impressed with your writing skills and also with the layout on your weblog. Is this a paid theme or did you modify it yourself? Either way keep up the nice quality writing, it’s rare to see a great blog like this one today..

  12. I want to express appreciation to this writer for bailing me out of this predicament. Because of looking through the the web and getting techniques that were not beneficial, I thought my life was well over. Existing without the presence of answers to the issues you have fixed by means of this blog post is a crucial case, and the kind which could have badly affected my career if I had not come across your website. Your personal ability and kindness in playing with the whole lot was precious. I am not sure what I would’ve done if I hadn’t discovered such a step like this. It’s possible to at this time look forward to my future. Thanks so much for this high quality and effective help. I won’t be reluctant to recommend your web sites to any person who should have care about this situation.

  13. I’m not sure where you are getting your info, but good topic. I needs to spend some time learning more or understanding more. Thanks for excellent info I was looking for this info for my mission.

  14. cork shops says:

    hey there and thank you for your info – I’ve certainly picked up something new from right here. I did however expertise several technical points using this site, since I experienced to reload the web site lots of times previous to I could get it to load properly. I had been wondering if your web host is OK? Not that I am complaining, but sluggish loading instances times will very frequently affect your placement in google and can damage your high-quality score if ads and marketing with Adwords. Anyway I am adding this RSS to my email and could look out for a lot more of your respective interesting content. Make sure you update this again soon..

  15. I wanted to compose a brief word to be able to express gratitude to you for these marvelous points you are sharing here. My long internet lookup has finally been rewarded with incredibly good insight to share with my co-workers. I ‘d admit that most of us site visitors actually are very much blessed to be in a fantastic community with so many brilliant people with very helpful solutions. I feel pretty happy to have encountered the web page and look forward to many more thrilling moments reading here. Thank you once again for all the details.

  16. I’m extremely impressed with your writing skills as well as with the layout on your blog. Is this a paid theme or did you customize it yourself? Either way keep up the excellent quality writing, it’s rare to see a nice blog like this one nowadays..

  17. CV says:

    I and my guys came checking out the good hints on the blog while at once I got a horrible suspicion I had not thanked you for them. All of the young boys are already thrilled to read through all of them and have now undoubtedly been taking pleasure in these things. Thanks for truly being considerably kind and for figuring out variety of brilliant issues millions of individuals are really eager to be informed on. My honest regret for not saying thanks to earlier.

  18. Hi there, You have done a fantastic job. I will certainly digg it and personally recommend to my friends. I’m confident they will be benefited from this website.

  19. I truly wanted to write a simple word in order to express gratitude to you for those precious tricks you are writing on this website. My time-consuming internet search has finally been recognized with awesome concept to share with my neighbours. I ‘d tell you that most of us readers are extremely blessed to be in a fine place with so many awesome individuals with very beneficial strategies. I feel truly blessed to have discovered your website page and look forward to many more entertaining moments reading here. Thank you once again for a lot of things.

  20. thanks, I believe your visitors could possibly want significantly more articles or blog posts similar to this carry on the superior perform.

  21. Thanks a lot this post, it is really fantastic!

  22. Muchos Gracias for your post.Really thank you! Cool.

  23. Adina Fagnan says:

    I need to to thank you for this excellent read!! I definitely enjoyed every bit of it. I’ve got you saved as a favorite to check out new stuff you post…

  24. Selim Raza says:

    It is not work to me ,I use it in the Java Class file ,I use it same ,but it not work ,please any help?

  25. FireFire Dekha says:

    yes,do not work.

Leave a Reply