Search code examples
androidmenuiconsactionbarsherlock

How to remove icons from an ActionBarSherlock's overflow menu on Android 2.3?


I have an app with ActionBarSherlock using theme Theme.Sherlock.Light.DarkActionBar. Action bar is dark and my menu icons are light. When I run my app on small layouts, 2 or 3 menu items with icons are displayed in the overflow menu.

On Android 3+ the overflow menu items will not display their icons, but on Android 2.3 and earlier I see menu tiles with almost invisible icons, because the tile color is white and icons are close to be white.

enter image description here

As you can see, the light icons are invisible on a white background, but they must have light color to be visible on a dark action bar:

enter image description here

Can I remove icons when menu items are displayed in the overflow menu?


Solution

  • I was also facing the same issue:

    there are many ways you can achieve this rather than removing image:

    1)you can use respective drawable folder to put light and dark image.

    2)You can also change the background color by code of your menu by checking your device version.

    If you device doen't support overflow menu, the, you can change the background color of your menu as well as you can also change menu text color.

    I was also facing the same issue and resolved using following one:

    static final Class<?>[] constructorSignature = new Class[] {Context.class, AttributeSet.class};
    
    class MenuColorFix implements LayoutInflater.Factory {
    public View onCreateView(String name, Context context, AttributeSet attrs) {
        if (name.equalsIgnoreCase("com.android.internal.view.menu.ListMenuItemView")) {
            try {
                Class<? extends ViewGroup> clazz = context.getClassLoader().loadClass(name).asSubclass(ViewGroup.class);
                Constructor<? extends ViewGroup> constructor = clazz.getConstructor(constructorSignature);
                final ViewGroup view = constructor.newInstance(new Object[]{context,attrs});
    
                new Handler().post(new Runnable() {
                    public void run() {
                        try {
                            view.setBackgroundColor(Color.BLACK);
                            List<View> children = getAllChildren(view);
                            for(int i = 0; i< children.size(); i++) {
                                View child = children.get(i);
                                if ( child instanceof TextView ) {
                                    ((TextView)child).setTextColor(Color.WHITE);
                                }
                            }
                        }
                        catch (Exception e) {
                            Log.i(TAG, "Caught Exception!",e);
                        }
    
                    }
                });
                return view;
            }
            catch (Exception e) {
                Log.i(TAG, "Caught Exception!",e);
            }
        }
        return null;
    }       
    }
    
    public List<View> getAllChildren(ViewGroup vg) {
    ArrayList<View> result = new ArrayList<View>();
    for ( int i = 0; i < vg.getChildCount(); i++ ) {
        View child = vg.getChildAt(i);
        if ( child instanceof ViewGroup) {
            result.addAll(getAllChildren((ViewGroup)child));
        }
        else {
            result.add(child);
        }
    }
    return result;
    }
    
    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
    LayoutInflater lInflater = getLayoutInflater();
    if ( lInflater.getFactory() == null ) {
        lInflater.setFactory(new MenuColorFix());
    }
    super.onCreateContextMenu(menu, v, menuInfo);
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.myMenu, menu);
    }
    

    3) change background color from styles.xml file

    <style name="Theme.MyTheme" parent="Theme.Sherlock.ForceOverflow">
        <item name="actionBarStyle">@style/Widget.MyTheme.ActionBar</item>
        <item name="android:actionBarStyle">@style/Widget.MyTheme.ActionBar</item>
    </style>
    
    <style name="Widget.MyTheme.ActionBar" parent="Widget.Sherlock.ActionBar">
        <item name="android:background">#ff000000</item>
        <item name="background">#ff000000</item>
    </style>
    

    For me, all of the 3 worked fine

    Hope, this will work for you as well