Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display menu item with icon and text in AppCompatActivity

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?

like image 629
Yarh Avatar asked Oct 06 '15 11:10

Yarh


People also ask

How do I show the menu icon on Android?

Click res → New → Vector Asset . Choose the icon that you want by clicking on the android icon, click “Next” button and then click “Finish”. 6- Now we can add android menu items with icons, we will have 4 menu items. 1 menu item will be the root while the other 3 menu items will be grouped under a single Menu .


1 Answers

@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

like image 74
Harshal Pathak Avatar answered Oct 12 '22 00:10

Harshal Pathak