How to get text on an ActionBar Icon?
Here is some example code that worked for me.
1: Create a layout for your badge menu item.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="48dp"
android:layout_height="fill_parent"
android:layout_gravity="right" >
<!-- Menu Item Image -->
<ImageView
android:layout_width="48dp"
android:layout_height="fill_parent"
android:clickable="true"
android:src="@drawable/bkg_actionbar_notify_off" />
<!-- Badge Count -->
<TextView
android:id="@+id/actionbar_notifcation_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:padding="@dimen/padding_small"
android:text="99"
android:textColor="@color/holo_orange_dark" />
</RelativeLayout>
2: Create a menu item in res/menu and set the actionLayout to your layout
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/badge"
android:actionLayout="@layout/actionbar_badge_layout"
android:icon="@drawable/icn_menu_posts"
android:showAsAction="always">
</item>
</menu>
3: Then in onCreateOptionsMenu of your activity or fragment you can do something like this...
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.badge, menu);
RelativeLayout badgeLayout = (RelativeLayout) menu.findItem(R.id.badge).getActionView();
TextView tv = (TextView) badgeLayout.findViewById(R.id.actionbar_notifcation_textview);
tv.setText("12");
}
Note: If you wanted to change the badge count later on, you could store a reference to the Menu object passed to onCreateOptionsMenu and use the same code to get the required view and set a value.
=== ApCompat Warning ==================================================
If using the AppCompatActivity then you must set the actionView in onCreateOptionsMenu
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu, menu);
MenuItem item = menu.findItem(R.id.badge);
MenuItemCompat.setActionView(item, R.layout.actionbar_badge_layout);
RelativeLayout notifCount = (RelativeLayout) MenuItemCompat.getActionView(item);
TextView tv = (TextView) notifCount.findViewById(R.id.actionbar_notifcation_textview);
tv.setText("12");
return super.onCreateOptionsMenu(menu);
attempting to display icon with text android ActionBar
Your code worked properly in Android 5.0 and above.
But in android 4.2 display only icon, not text!!
So if you worked on below 5.0 then use actionLayout
.
Refer this answer:
How to get text on an ActionBar Icon?
Hope this will help you
Android: How to write text under the icon in the actionbar
'always|withText' only works if there is sufficient room. Else, it will only place icon. In your case, you've got several icons there which does not leave any spare room for text.
You've got two options:
- Reduce the icons by moving them either to an overflow menu or somewhere else in your UI.
- Design images which consist both icon and text and use them as your icons.
Edit: Well, here's a lot better answer: https://stackoverflow.com/a/12225863/2511884
ActionBar menu with text ,icon,text
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:[yourapp]="http://schemas.android.com/apk/res-auto">
<item android:id="@+id/menu_add_size"
android:title="@string/menu_add_item"
android:orderInCategory="10"
[yourapp]:showAsAction="always"
android:icon="@android:drawable/ic_menu_add" />
instead of [yourapp] type your app name
How to add text to icons in ActionBar?
Use android:showAsAction="ifRoom|withText"
to show the menu title along with the icon.
Related Topics
How to Get a Working Vertical Seekbar in Android
How to Play Youtube Video in My Android Application
How to Create a Popup Window (Popupwindow) in Android
How to Tell If 'Mobile Network Data' Is Enabled or Disabled (Even When Connected by Wifi)
Using Getresources() in Non-Activity Class
Continue Service Even If Application Is Cleared from Recent App
Android - Linearlayout Horizontal with Wrapping Children
Question Mark () in Xml Attributes for Android
Detect Scroll Up & Scroll Down in Listview
Android Sharedpreference Security
Get Only Email Address from Contact List Android
How to Enable Disable Gps Programmatically in Android
Android: I Lost My Android Key Store, What Should I Do
How to Create a Simple Divider in the New Navigationview
Missing "<Sdk>/Extras/Google/Google_Play_Services/Libproject" Folder After Update to Revision 30