40

I tried different combinations in xml file:

<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/action_create_alarm"
        android:icon="@drawable/ic_action_accept"
        android:orderInCategory="100"
        android:title="@string/menu_create_alarm"
        app:showAsAction="ifRoom|withText" />
</menu>

or

<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/action_create_alarm"
        android:icon="@drawable/ic_action_accept"
        android:orderInCategory="100"
        android:title="@string/menu_create_alarm"
        app:showAsAction="always|withText" />
</menu>

or

<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/action_create_alarm"
        android:icon="@drawable/ic_action_accept"
        android:orderInCategory="100"
        android:title="@string/menu_create_alarm"
        app:showAsAction="withText" />
</menu>

or

<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/action_create_alarm"
        android:icon="@drawable/ic_action_accept"
        android:orderInCategory="100"
        android:title="@string/menu_create_alarm"
        android:showAsAction="always|withText" />
</menu>

I tried to set it programmaticly

@Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater){
        MenuItem item = menu.add(R.string.menu_create_alarm);
        item.setShowAsAction(MenuItem.SHOW_AS_ACTION_WITH_TEXT|MenuItem.SHOW_AS_ACTION_IF_ROOM);
        item.setIcon(R.drawable.ic_action_accept);
        item.setOnMenuItemClickListener(
            new OnMenuItemClickListener(){

                @Override
                public boolean onMenuItemClick(MenuItem item){
                    saveAlarm();
                    return true;
                }
            }
        );


//      inflater.inflate(R.menu.menu_create_alarm, menu);
        super.onCreateOptionsMenu(menu, inflater);

    }

or

<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/action_create_alarm"
        android:icon="@drawable/ic_action_accept"
        android:orderInCategory="100"
        android:title="@string/menu_create_alarm"
        android:showAsAction="always|withText"
        app:showAsAction="always|withText" />
</menu>

However, Only Icon appears. And there is planty of room, cause I did not set toolbar title. Removing menues and replasing them with button inside toolbar is not sutable.
How to display text?

Yarh
  • 4,116
  • 4
  • 39
  • 88

9 Answers9

27
@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
//        getMenuInflater().inflate(R.menu.menu_patient_home_screen, menu);


        menu.add(0, 1, 1, menuIconWithText(getResources().getDrawable(R.mipmap.user_2), getResources().getString(R.string.action_profile)));
        menu.add(0, 2, 2, menuIconWithText(getResources().getDrawable(R.mipmap.add_user), getResources().getString(R.string.action_add_user)));
        menu.add(0, 3, 3, menuIconWithText(getResources().getDrawable(R.mipmap.switch_profile), getResources().getString(R.string.action_switch_profile)));
        menu.add(0, 4, 4, menuIconWithText(getResources().getDrawable(R.mipmap.logout), getResources().getString(R.string.action_sign_out)));
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        switch (item.getItemId()) {
            case 1:
                Toast.makeText(PatientHomeScreen.this, "Profile is Clicked", Toast.LENGTH_SHORT).show();
                return true;
            case 2:
                Toast.makeText(PatientHomeScreen.this, "Add New User is Clicked", Toast.LENGTH_SHORT).show();
                return true;
            case 3:
                Toast.makeText(PatientHomeScreen.this, "Switch Profile is Clicked", Toast.LENGTH_SHORT).show();
                return true;
            case 4:
                Toast.makeText(PatientHomeScreen.this, "Sign Out is Clicked", Toast.LENGTH_SHORT).show();
                return true;
        }

        return super.onOptionsItemSelected(item);
    }

    private CharSequence menuIconWithText(Drawable r, String title) {

        r.setBounds(0, 0, r.getIntrinsicWidth(), r.getIntrinsicHeight());
        SpannableString sb = new SpannableString("    " + title);
        ImageSpan imageSpan = new ImageSpan(r, ImageSpan.ALIGN_BOTTOM);
        sb.setSpan(imageSpan, 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

        return sb;
    }

Screen shot of emulator

Hope this will help you

Harshal Pathak
  • 689
  • 6
  • 18
  • 1
    Above solution works good, If you are working in Fragment you need to change only one thing, change onCreateOptionsMenu(Menu menu) with onCreateOptionsMenu(Menu menu, MenuInflater inflater). and add setHasOptionsMenu(true) in onCreate(Bundle savedInstanceState) or View onCreateView(LayoutInflater inflater....) – Rajan Maurya Dec 15 '17 at 08:52
23

You need to add tools:context="your class" to menu tag

menu.xml

<?xml version="1.0" encoding="utf-8"?>
<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=".activities.BaseActivity">


   <item
       android:id="@+id/action_notification1"
       android:icon="@drawable/three"
       android:title="action_notification"
       app:showAsAction="always">
       <menu>
           <item
               android:id="@+id/profile"
               android:icon="@drawable/profile"
               android:orderInCategory="100"
               android:title="PROFILE" />

           <item
               android:id="@+id/c"
               android:icon="@drawable/correct_tick"
               android:orderInCategory="100"
               android:title="COMPLETED TRIPS" />

           <item
               android:id="@+id/app"
               android:icon="@drawable/report_issue"
               android:orderInCategory="100"
               android:title="REPORT ISSUES" />
           <item
               android:id="@+id/r"
               android:icon="@drawable/correct_tick"
               android:orderInCategory="100"
               android:title="REACHED CENTER" />


           <item
               android:id="@+id/pdf"
               android:icon="@drawable/pdf_image"
               android:orderInCategory="100"
               android:title="BAG INFO" />
           <item
               android:id="@+id/l"
               android:icon="@drawable/logout"
               android:orderInCategory="100"
               android:title="LOGOUT" />
       </menu>
   </item>


</menu>

Override onCreateOptionsMenu() Method

@Override 
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu); 
    menu.getItem(0).getSubMenu().getItem(3).setVisible(false);
    menu.getItem(0).getSubMenu().getItem(4).setVisible(true);
    return super.onCreateOptionsMenu(menu);
} 

You should write tool:context to menu tag then run you will get icons before your text.

Letsintegreat
  • 2,764
  • 4
  • 16
  • 35
Raj Kumar
  • 648
  • 7
  • 17
19

Adding to the answer from dev_ry, there is a much smoother way without using reflection by just casting and suppressing the restricted API warning:

import android.support.v7.view.menu.MenuBuilder;

@SuppressLint("RestrictedApi")
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    if (menu instanceof MenuBuilder) {
        ((MenuBuilder) menu).setOptionalIconsVisible(true);
    }
    getMenuInflater().inflate(R.menu.menu, menu);
    return super.onCreateOptionsMenu(menu);
}
Community
  • 1
  • 1
Maxr1998
  • 922
  • 9
  • 21
12
protected boolean onPrepareOptionsPanel(View view, Menu menu) {
    if (menu != null) {
        if (menu.getClass().getSimpleName().equals("MenuBuilder")) {
            try {
                Method m = menu.getClass().getDeclaredMethod(
                        "setOptionalIconsVisible", Boolean.TYPE);
                m.setAccessible(true);
                m.invoke(menu, true);
            } catch (Exception e) {
                Log.e(getClass().getSimpleName(),
                        "onMenuOpened...unable to set icons for overflow menu",
                        e);
            }
        }
    }
    return super.onPrepareOptionsPanel(view, menu);
}
Ravi Yadav
  • 1,948
  • 2
  • 23
  • 30
  • When my class extended Activity, this worked form me: http://stackoverflow.com/a/22288914/555762. But when i changed my class to extend AppCompatActivity, previous solution stoped working and this answer saved my day =) – Uroš Podkrižnik Aug 16 '16 at 07:40
  • Thanks a lot for this. Any change we need to make in proguard? – Atul Jan 17 '17 at 06:44
  • 1
    @Atul I have not tested it for proguard but dont think that progurad need some changes because of this. – Ravi Yadav Jan 17 '17 at 06:47
  • Tried everything else to get both the icon and text to display in the drop-down menu. This worked which also gave an error which was resolved by adding @SuppressLint("RestrictedApi") before the method as per https://stackoverflow.com/questions/41150995/appcompatactivity-oncreate-can-only-be-called-from-within-the-same-library-group – Mickey D Apr 28 '18 at 13:34
9

xml:

<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/action_create_alarm"
        android:icon="@drawable/ic_action_accept"
        android:orderInCategory="100"
        android:title="@string/menu_create_alarm"
        android:showAsAction="always|withText"
        app:showAsAction="always|withText" />
</menu>

java:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    new MenuInflater(this).inflate(R.menu.menu_frg_safetybox, menu);
    super.onCreateOptionsMenu(menu, inflater);
}
Nicolas Tyler
  • 9,372
  • 4
  • 38
  • 62
  • 1
    there is still no text, only icon – Yarh Oct 06 '15 at 12:27
  • Android decides for itself at the end of the day if it can fit or not. – Nicolas Tyler Oct 06 '15 at 12:29
  • how to call, new inflator in fragment? If i do @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater){ // inflater.inflate(R.menu.menu_create_level, menu); new MenuInflater(getActivity()).inflate(R.menu.menu_create_level, menu); super.onCreateOptionsMenu(menu, inflater); }, I still have only icon – Yarh Oct 06 '15 at 12:31
  • As i said. if the OS decides that the text and icon are to large for the device, it will only put the icon. You would have to try a larger device. – Nicolas Tyler Oct 06 '15 at 12:36
  • but how is this diff from mine lol.. life on stackoverflow :) – Elltz Oct 06 '15 at 12:51
6

'always|withText' will work if there is sufficient room, otherwise it will place only the icon.You an see that by rotating phone to landscape mode..

       <item
        android:id="@+id/action_create_alarm"
        android:icon="@drawable/ic_launcher"
        android:title="Save"
        app:showAsAction="always|withText"
        />
sm_
  • 2,896
  • 2
  • 10
  • 21
  • 3
    that is correct. If I turn the device into landscape mode the text show up. Nevertheless I want to text is being displaying always. Although my Title in toolbar is very short ("Activity") text doesn't appear. – murt Apr 06 '16 at 14:11
6

You can use following code for establishing your purpose. No need to make it programmatic. You can trick in menu.xml file.

menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

<item
    android:id="@+id/overflowMenu"
    android:icon="@drawable/ic_more_vert_white_24dp"
    android:title=""
    app:showAsAction="always">

    <menu>
        <item
            android:id="@+id/menuWithIconText"
            android:icon="@drawable/chosen_icon"
            android:title="@string/menu_item_title"
            />
    </menu>
</item>

In above code, we've used menu inside of item. In inside menu, we've defined our overflow menu which we want with icon. Just remember to use app:showAsAction="always" in outside item tag.

Rohit Sawai
  • 447
  • 7
  • 23
  • I don't have such a drawable in my project and when i add it it says that it's not found. – SubqueryCrunch Feb 23 '21 at 13:21
  • @SubqueryCrunch drawable is folder where you save icons. Basically in drawable folder you keep all your assets. Please check your assets folder and select it in android:icon attribute. – Rohit Sawai Feb 27 '21 at 13:11
3

try these two together

app:showAsAction="always|withText"
android:showAsAction="always|withText"
Elltz
  • 10,073
  • 3
  • 26
  • 55
1

MyActivity.java:

@Override
    public boolean onCreateOptionsMenu(Menu menu) 
    {
        getMenuInflater().inflate(R.menu.my_menu, menu);
        View view = menu.findItem(R.id.whats_this).getActionView();
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // do something
            }
        });
        return super.onCreateOptionsMenu(menu);
    }

my_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<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=".MyActivity">

    <item
        android:id="@+id/item"
        app:actionLayout="@layout/my_layout"
        android:orderInCategory="100"
        android:title="@string/whats_this"
        app:showAsAction="always" />
</menu>

my_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="30dp"
        android:paddingLeft="5dp"
        android:gravity="center_vertical"
        android:textStyle="bold"
        android:textColor="@color/white"
        android:text="@string/whats_this"/>

    <ImageView
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:src="@mipmap/question"/>

</LinearLayout>
Sunny Jha
  • 61
  • 6