0

OptionsMenu is not showing Image, but sub menu Show Image. Is it possible to show image in OptionsMenu. In previous version Show Image in OptionsMenu.

New Version like v4.0, v4.2,v4.4 is not Show Image in OptionsMenu.

Please help me I want to show the image in all Version Device.

This is my code:

MenuItem menu1 = menu.add(Menu.NONE, MENU1, 1, "Home");
menu1.setIcon(R.drawable.ic_launcher);
menu1.setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_NEVER);

MenuItem menu2 = menu.add(Menu.NONE, MENU2, 2, "Upload");
menu2.setIcon(R.drawable.ic_launcher);
menu2.setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_NEVER);

MenuItem menu3 = menu.add(Menu.NONE, MENU3, 3, "Log out");
menu3.setIcon(R.drawable.ic_launcher);
menu3.setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_NEVER);

Thank you very much for your time and assistance in this matter.

ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96

3 Answers3

0

Because logos in menu work only on older platforms. Currently, you can't show icons in a menu item if you're running on Android 3.0+.

You can show logos in menu only with custom Action Bar, e.g. Sherlock Library.

Look here, maybe you will find some workaround.

Pang
  • 9,564
  • 146
  • 81
  • 122
Eliasz Kubala
  • 3,836
  • 1
  • 23
  • 28
0

In portrait mode, either it shows icon or the text. It's not possible to show both at the same time unless your device has sufficient screen space.

Also, alter you code to allow both text and icon to be shown (if possible).

menu1.setShowAsAction(MenuItem.SHOW_AS_ACTION_WITH_TEXT);

For more info, read this.

Community
  • 1
  • 1
waqaslam
  • 67,549
  • 16
  • 165
  • 178
0

Here you go..

  1. Add menu.xml under your draw folder. Use below code for menu tag. Here, I have used own icons, you can replace with your icons.

    <menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity">
    
        <item
            android:id="@+id/more"
            android:icon="@drawable/overflow"
            app:showAsAction="always|withText"
            android:title="More">
                <menu>
                        <item
                            android:id="@+id/next"
                            android:icon="@drawable/next"
                            app:showAsAction="always|withText"
                            android:title="Next">
                        </item>
                        <item
                            android:id = "@+id/prev"
                            android:icon="@drawable/prev"
                            app:showAsAction="always|withText"
                            android:title="Previous"/>
    
                </menu>
        </item>
    

2.Inside your MainActivity.java add below code

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater menuInflater = getMenuInflater();
        menuInflater.inflate(R.menu.menu,menu);
       /* menu.add("Add Contacts");
        menu.getItem(0).setIcon(R.drawable.prev); */
        return super.onCreateOptionsMenu(menu);
    }
  1. We are manually adding icons. So be careful to add icons.

Is this what you want?

Output will look like..

enter image description here

Flutterian
  • 1,761
  • 1
  • 21
  • 47