0

I would like to display badges on menu items. How do I do it?

Basically, I don't want to draw or use the canvas to do so.

4
  • do you only want the images associated with the your menu items..
    – Arpit Garg
    Mar 22, 2012 at 19:07
  • yes, A bitmap and a number on top right corner Mar 22, 2012 at 19:15
  • If you desire the things as that of messages icon.. Then it is complex task and need to be handled via Custom widget,, You can simple have an button with image in that form with certain text and gravity as top|right etc
    – Arpit Garg
    Mar 22, 2012 at 19:26
  • @AgarwalShankar did you ever figure out how to do this?
    – Adam Johns
    Oct 4, 2013 at 15:42

3 Answers 3

1

You could try creating a LayerListDrawable, with your regular icon as the first layer and your badge as the second layer, then use that with setIcon() on MenuItem.

0
0

The options menu in Android can be customized to set the background or change the text appearance. The background and text color in the menu couldn’t be changed using themes and styles.

The Android source code (data\res\layout\icon_menu_item_layout.xml) uses a custom item of class “com.android.internal.view.menu.IconMenuItem”View for the menu layout. We can make changes in the above class to customize the menu. To achieve the same, use the LayoutInflater factory class and set the background and text color for the view.

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.my_menu, menu);
    getLayoutInflater().setFactory(new Factory() {
        @Override
        public View onCreateView(String name, Context context, AttributeSet attrs) {
            if (name .equalsIgnoreCase(“com.android.internal.view.menu.IconMenuItemView”)) {
                try{
                    LayoutInflater f = getLayoutInflater();
                    final View view = f.createView(name, null, attrs);
                    new Handler().post(new Runnable() {
                        public void run() {
                            // Set the background drawable
                            view .setBackgroundResource(R.drawable.my_ac_menu_background);

                            // Set the text color
                            ((TextView) view).setTextColor(Color.WHITE);
                        }
                    });
                    return view;
                }
                catch (InflateException e) {
                }
                catch (ClassNotFoundException e) {
                }
            }
            return null;
        }
    });
    return super.onCreateOptionsMenu(menu);
}
1
  • This helped me but it was not what exactly i was looking Apr 1, 2012 at 17:07
0

Menuitem has an attribute icon, for example:

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:id="@+id/mi_main_preferences"
        android:title="@string/cmd_preferences"
        android:icon="@android:drawable/ic_menu_preferences">
    </item>
</menu>

Above example uses system icon (preferences menu).

2
  • But how can i have bbadge on it Mar 22, 2012 at 19:17
  • I'm sorry, my English is not good. Is there difference between icon and badge?
    – user942821
    Mar 23, 2012 at 0:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.