How to create a textview in the res \ menu?

I am trying to create a TextView in a menu, the text should be set dynamically. Right now, my menu has the following:

<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android" > <item android:id="@+id/action_search" android:actionViewClass="android.widget.SearchView" android:icon="@android:drawable/ic_menu_search" android:showAsAction="ifRoom" android:title="Search"/> <item android:id="@+id/action_sync" android:icon="@drawable/ic_menu_refresh" android:showAsAction="ifRoom" android:title="Sync"/> </menu> 

How to create an element with text?

+4
source share
2 answers

If you want to just show the text, just do not add the icon to menu.xml. If you want to change the text in the code, you can do this in onPrepareOptionsMenu () in the activity callback as such:

 @Override public boolean onPrepareOptionsMenu(Menu menu) { MenuItem myItem = menu.findItem(R.id.the_item); myItem.setTitle("bla"); return true; } 
+3
source

I think that if you want to configure settings such as color, text size or font, you should have a textual representation:

  String title = "MyTitle"; TextView tvMiTitle = new TextView(getActivity()); tvMiTitle.setText(title); tvMiTitle.setTextColor(Color.WHITE); // == // MenuItem myMenuItem; myMenuItem.setActionView(tvMiTitle); 
+1
source

All Articles