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 (data\res\layout\icon_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);
}

tett

  • Black Daniel

    February 17, 2015

    that has not worked for me, my project targets min sdk 8.

  • FireFire Dekha

    June 11, 2014

    yes,do not work..

  • Selim Raza

    June 11, 2014

    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?.

  • Aqua Render

    December 19, 2013

    doesn’t work.

  • mortgage

    December 2, 2013

    Here is my weblog mortgage.

  • Adina Fagnan

    October 27, 2013

    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….

  • binary insider

    October 9, 2013

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

  • Clotilde Inkavesvanitc

    August 1, 2013

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

  • Rudolf Birnie

    July 26, 2013

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

  • Business Tax Advisor in Bondi Junction

    July 6, 2013

    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..

  • snow conditions

    July 6, 2013

    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..

  • CV

    July 5, 2013

    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..

  • Amazon Fat Burner

    July 4, 2013

    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...

  • auckland activities

    July 4, 2013

    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..

  • cork shops

    July 3, 2013

    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...

  • Nutritional supplements

    July 1, 2013

    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..

  • Lovely Pictures

    July 1, 2013

    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..

  • Septic Cleaning in Conyers Georgia

    June 30, 2013

    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...

  • Auto relocation

    June 30, 2013

    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!.

  • media stream

    June 29, 2013

    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..

  • Jyothi

    May 4, 2012

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

  • sanjeet ajnabee

    April 28, 2012

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

  • Andrew

    April 4, 2012

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

  • udit

    March 3, 2012

    doesn't work.

  • Ali

    January 30, 2012

    It doesn't work... Always end with exception....

  • bo

    January 9, 2012

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

  • jetti Madhu

    June 29, 2011

    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?.

  • Web Design Company bangalore

    April 28, 2011

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

  1. Web Design Company bangalore

    April 28, 2011

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

  2. jetti Madhu

    June 29, 2011

    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

    January 9, 2012

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

  4. Ali

    January 30, 2012

    It doesn’t work… Always end with exception…

  5. udit

    March 3, 2012

    doesn’t work

  6. Andrew

    April 4, 2012

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

  7. sanjeet ajnabee

    April 28, 2012

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

  8. Jyothi

    May 4, 2012

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

  9. media stream

    June 29, 2013

    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. Auto relocation

    June 30, 2013

    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. Septic Cleaning in Conyers Georgia

    June 30, 2013

    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. Lovely Pictures

    July 1, 2013

    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. Nutritional supplements

    July 1, 2013

    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

    July 3, 2013

    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. auckland activities

    July 4, 2013

    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. Amazon Fat Burner

    July 4, 2013

    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

    July 5, 2013

    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. snow conditions

    July 6, 2013

    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. Business Tax Advisor in Bondi Junction

    July 6, 2013

    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. Rudolf Birnie

    July 26, 2013

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

  21. Clotilde Inkavesvanitc

    August 1, 2013

    Thanks a lot this post, it is really fantastic!

  22. binary insider

    October 9, 2013

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

  23. Adina Fagnan

    October 27, 2013

    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. mortgage

    December 2, 2013

    Here is my weblog mortgage

  25. Aqua Render

    December 19, 2013

    doesn’t work

  26. Selim Raza

    June 11, 2014

    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?

  27. FireFire Dekha

    June 11, 2014

    yes,do not work.

  28. Black Daniel

    February 17, 2015

    that has not worked for me, my project targets min sdk 8

share your thoughts

Leave a Reply