19

I'm trying to change the standard light grey to a light green. Seems that there is not a simple way to do this (through Android Themes, for example) but I have found a workaround as explained at this page: http://tinyurl.com/342dgn3.

The author seems disappeared, can someone help me integrating this code? I don't understand where I need to implement the LayoutInflater factory class.

rciovati
  • 27,603
  • 6
  • 82
  • 101
  • If someone is interested i've solved. http://pastebin.com/1QHGTMUW just call the setMenuBackground in the onCreate – rciovati Apr 27 '10 at 21:11
  • Here's how I got custom Action Bar menu background colour working on Android 4.0+: http://stackoverflow.com/a/20077381/56285 – Jonik Nov 19 '13 at 17:10
  • i try you code, but it first tell me to remove @Override and then nothings hapens. I tried to change the menue back ground color. – Mahdi Mar 15 '14 at 14:53
  • it's wont work on 4.3 api why? – Mahdi Mar 15 '14 at 15:14

3 Answers3

11

When ur are inflating the menu call this setMenuBackground() method

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

and write this in the setMenuBackground() method

    protected void setMenuBackground(){                     
        // Log.d(TAG, "Enterting setMenuBackGround");  
        getLayoutInflater().setFactory( new Factory() {  
            public View onCreateView(String name, Context context, AttributeSet attrs) {
                if ( name.equalsIgnoreCase( "com.android.internal.view.menu.IconMenuItemView" ) ) {
                    try { // Ask our inflater to create the view  
                        LayoutInflater f = getLayoutInflater();  
                        final View view = f.createView( name, null, attrs );  
                        /* The background gets refreshed each time a new item is added the options menu.  
                        * So each time Android applies the default background we need to set our own  
                        * background. This is done using a thread giving the background change as runnable 
                        * object */
                        new Handler().post( new Runnable() {  
                            public void run () {  
                                // sets the background color   
                                view.setBackgroundResource( R.color.androidcolor);
                                // sets the text color              
                                ((TextView) view).setTextColor(Color.BLACK);
                                // sets the text size              
                                ((TextView) view).setTextSize(18);
                }
                        } );  
                    return view;
                }
            catch ( InflateException e ) {}
            catch ( ClassNotFoundException e ) {}  
        } 
        return null;
    }}); 
}
Jitendra
  • 1,015
  • 9
  • 24
Abhay Kumar
  • 1,582
  • 1
  • 19
  • 45
  • 1
    Has anyone gotten this to work? Seems to have no effect (Gingerbread) – Chad Schultz May 31 '12 at 18:42
  • It works with android 2.2 haven't checked with 2.3 – Abhay Kumar Jun 01 '12 at 03:55
  • 3
    Doesn't seem to work for me (on Android 4.0+). `com.android.internal.view.menu.IconMenuItemView`does not match anything, and when I changed that to `com.android.internal.view.menu.ListMenuItemView`, my app just crashes when opening the menu. With `TextView` I got it to change *a part* of menu items' background color, but not everything. In every case, this seems hacky and prone to breaking with future Android versions. – Jonik Nov 19 '13 at 15:35
  • the only thing I get is "java.lang.illegalstateexception: a factory has already been set on this layoutinflater" for android 2.3+ and 4.0+ devices. – MSIslam Jan 07 '14 at 21:39
  • It's wont work for me too .@Jonik did you solve your problem? so let me know – Mahdi Mar 15 '14 at 13:41
  • 1
    @AbhayKumar can you please describe about 'imports'. I am getting `java.lang.illegalstateexception: a factory has already been set on this layoutinflater` error. please help me. – maddy d Jul 22 '14 at 07:40
  • Same error has shown to me please recommend something to solve this one :) – Syed Hamza Hassan May 06 '16 at 10:58
  • any update for androidx and kotlin ? – Maveňツ May 10 '19 at 07:44
5

This is clearly a problem that a lot of programmers have and to which Google has yet to provide a satisfactory, supported solution.

The setMenuBackground() hack which Abhay Kumar and Nik Reiman posted is a good start but it either crashes or does not work on Android 2.3.

Please see my answer (Louis Semprini) in this stackoverflow question for a better commented and more refined hack that works on 2.1, 2.2, and 2.3 (and that should do no harm on 3.X, though we can never guarantee this):

How to change the background color of the options menu?

Also, here are many other resources you may find helpful for this question:

Change background color of android menu

Android: customize application's menu (e.g background color)

http://www.macadamian.com/blog/post/android_-_theming_the_unthemable/

Android MenuItem Toggle Button

Is it possible to make the Android options menu background non-translucent?

http://www.codeproject.com/KB/android/AndroidMenusMyWay.aspx

Setting the menu background to be opaque

Community
  • 1
  • 1
Louis Semprini
  • 3,515
  • 2
  • 30
  • 31
0

Use the setMenuBackground in onCreate.

locoboy
  • 38,002
  • 70
  • 184
  • 260