Change text Size of menu item in android

I use simple menu items in the action bar, using the following code in the main action:

package com.kaasib.ftpclient; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.app.ActionBar; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item){ boolean ret; if(item.getItemId() == R.id.connection_manager){ ret = true; }else{ ret = super.onOptionsItemSelected(item); } return ret; } } 

Here is the xml menu in main.xml:

  <menu xmlns:android="http://schemas.android.com/apk/res/android" > <item android:id="@+id/connection_manager" android:orderInCategory="100" android:showAsAction="collapseActionView" android:title="@string/connection_manager" android:textSize="2sp" /> </menu> 

It works, except that it does not change the size of the text. Right now the text size for the menu item is larger and the font size is smaller. So what am I doing wrong? Shouldn't android:textSize attribute work? Or is there another way to do this? I believe that the text size should be set from XML, not from java, as this is related to design. Any suggestion?

+14
android android-layout android-menu
source share
7 answers

Add the "android: actionMenuTextAppearance" element for your actions in your styles. xml:

 <style name="AppThemeActivity" parent="Theme.AppCompat.Light"> <item name="android:actionMenuTextAppearance">@style/yourstyle</item> ... </style> 

Apply this style to your manifest activity:

 <activity android:theme="@style/AppThemeActivity" .../> 
+8
source share

So this is my solution, you can use SpannableString to extract the text, and then change the font through RelativeSizeSpan (if you want the text size to be the default) or through AbsoluteSizeSpan (if you want to manually enter the text size):

 public boolean onCreateOptionsMenu(Menu menu) { super.onCreateOptionsMenu(menu); MenuInflater awesome = getMenuInflater(); awesome.inflate(R.menu.menu_main, menu); for(int i = 0; i < menu.size(); i++) { MenuItem item = menu.getItem(i); SpannableString spanString = new SpannableString(menu.getItem(i).getTitle().toString()); int end = spanString.length(); spanString.setSpan(new RelativeSizeSpan(1.5f), 0, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); item.setTitle(spanString); } return true; } 

In this example, the text size of the menu items is increased by 50%.

+5
source share

Check out the following link! You can see how to create your own menu items!

Custom Menu Items

+3
source share

It has been a long time since it was asked, but I like this solution because steven smiths responds to changes of every kind:

Add this line to your NavigationView:

 app:itemTextAppearance="@style/MenuItems" 

And this is the styles:

 <style name="MenuItems" parent="AppTheme"> <item name="android:textSize">18sp</item> <item name="android:fontFamily">sans-serif-light</item> </style> 
+3
source share

add to the xml style file, for example, this bottom line of code, your own font size,

after that

 <style name="menu_text_style" parent="@android:style/TextAppearance.DeviceDefault.Widget.ActionBar.Menu"> <item name="android:textSize">16sp</item> <item name="android:textColor">@color/tab_default_color</item> <item name="android:textAllCaps">false</item> </style> 

after "menu_text_style" adds to the navigation menu

  app:itemTextAppearance="@style/menu_text_style" 
+3
source share

Or you can set the font size in the values ​​/ styles element for the theme you are using. In my case, the style specified in the AndroidManifest.xml file:

 android:theme="@style/AppTheme" 

So my style for AppTheme is now:

 <style name="AppTheme" parent="AppBaseTheme"> <item name="android:textSize">20sp</item> </style> 

It seems strange that it does not work for the element, though ...

0
source share

Works well, on the old API also:

In your style.xml :

 <style name="AppTheme" parent="Theme.AppCompat.Light"> <item name="actionMenuTextAppearance">@style/ActionMenuTextAppearance</item> <item name="android:actionMenuTextAppearance">@style/ActionMenuTextAppearance</item> </style> <style name="ActionMenuTextAppearance" parent="@style/TextAppearance.AppCompat.Widget.ActionBar.Menu"> <item name="android:textSize">16sp</item> <item name="android:textColor">#1a1a1a</item> </style> 

In your manifest :

 <activity android:name=".MainActivity" android:theme="@style/AppTheme" /> 
0
source share

All Articles