Search code examples
androidactionbarsherlock

Change bottom menu item background


How can I change the background of the menu Item which appears at the bottom, after clicking device menu button

enter image description here

I am using ActionBarSherlock and the top ActionBar has a Blue background. Thought the same will be the background for the bottom menu. But its white. Looks like default. So, the icon doesn't look good on that white background. How can I change it to blue.

I tried this:

<item name="android:panelBackground">@drawable/blue</item>

But didn't work.

Thank You.


Solution

  • I had a similar requirements and i found this working solution. Use the following

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu, menu);
        addOptionsMenuHackerInflaterFactory();
        return true;
    }
    
    @SuppressWarnings("rawtypes")
    static Class       IconMenuItemView_class = null;
    @SuppressWarnings("rawtypes")
    static Constructor IconMenuItemView_constructor = null;
    
    // standard signature of constructor expected by inflater of all View classes
    @SuppressWarnings("rawtypes")
    private static final Class[] standard_inflater_constructor_signature = 
    new Class[] { Context.class, AttributeSet.class };
    
    protected void addOptionsMenuHackerInflaterFactory()
    {
        final LayoutInflater infl = getLayoutInflater();
    
        infl.setFactory(new Factory()
        {
            public View onCreateView(final String name, 
                                     final Context context,
                                     final AttributeSet attrs)
            {
                if (!name.equalsIgnoreCase("com.android.internal.view.menu.IconMenuItemView"))
                    return null; // use normal inflater
    
                View view = null;
    
                if (IconMenuItemView_class == null)
                {
                    try
                    {
                        IconMenuItemView_class = getClassLoader().loadClass(name);
                    }
                    catch (ClassNotFoundException e)
                    {
                        // this OS does not have IconMenuItemView - fail gracefully
                        return null; // hack failed: use normal inflater
                    }
                }
                if (IconMenuItemView_class == null)
                    return null; // hack failed: use normal inflater
    
                if (IconMenuItemView_constructor == null)
                {
                    try
                    {
                        IconMenuItemView_constructor = 
                        IconMenuItemView_class.getConstructor(standard_inflater_constructor_signature);
                    }
                    catch (SecurityException e)
                    {
                        return null; // hack failed: use normal inflater
                    }
                    catch (NoSuchMethodException e)
                    {
                        return null; // hack failed: use normal inflater
                    }
                }
                if (IconMenuItemView_constructor == null)
                    return null; // hack failed: use normal inflater
    
                try
                {
                    Object[] args = new Object[] { context, attrs };
                    view = (View)(IconMenuItemView_constructor.newInstance(args));
                }
                catch (IllegalArgumentException e)
                {
                    return null; // hack failed: use normal inflater
                }
                catch (InstantiationException e)
                {
                    return null; // hack failed: use normal inflater
                }
                catch (IllegalAccessException e)
                {
                    return null; // hack failed: use normal inflater
                }
                catch (InvocationTargetException e)
                {
                    return null; // hack failed: use normal inflater
                }
                if (null == view) // in theory handled above, but be safe... 
                    return null; // hack failed: use normal inflater
    
    
                // apply our own View settings after we get back to runloop
                // - android will overwrite almost any setting we make now
                final View v = view;
                new Handler().post(new Runnable()
                {
                    public void run()
                    {
                        v.setBackgroundDrawable(getResources().getDrawable(R.drawable.menubuttonselector));
                             // here use your own selector drawable 
                        try
                        {
                            // in Android <= 3.2, IconMenuItemView implemented with TextView
                            // guard against possible future change in implementation
                            TextView tv = (TextView)v;
                            tv.setTextColor(Color.WHITE);
                        }
                        catch (ClassCastException e)
                        {
                            // hack failed: do not set TextView attributes
                        }
                    }
                });
    
                return view;
            }
        });
    }
    

    Caution:

    it is a hacking solution . As i needed it once I could not give enough time to find if there is any side effect. But it served me perfectly. Hoping it will be helpful for you