-1

I am trying to implement Option menu with icons but i m trying to set background on option menu but not succeeded how to achieve please help me

Sambhaji Karad
  • 387
  • 7
  • 18
  • possible duplicate of [Change the background color of the options menu](http://stackoverflow.com/questions/2944244/change-the-background-color-of-the-options-menu) – appoll May 04 '15 at 07:00

2 Answers2

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




  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;
}}); 
}
Mahi
  • 1,754
  • 2
  • 16
  • 37
0

See ActionBarStyleGenerator

Wher you need to set Popup color which will change the menu background as you want. Download generated style and apply in your project.

Harin
  • 2,413
  • 3
  • 15
  • 30