Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change background color of android menu [duplicate]

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.

like image 712
rciovati Avatar asked Apr 27 '10 06:04

rciovati


People also ask

How can I change the color of my menu bar in Android?

Just go to res/values/styles.edit the xml file to change the color of action bar.

How do I change the color of my header on Android?

First you have to change in manifest where you have declared the activity . Change app theme style to noActionBar. Now come to your main activity and initialize the new toolbar and set the color in it.


1 Answers

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;
    }}); 
}
like image 163
Abhay Kumar Avatar answered Sep 18 '22 17:09

Abhay Kumar