23

I am using simple menu items in action bar by using following code in main activity:

    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 menu xml 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 is working except it is not making any change to text size. Right now text size for menu item is bigger while I want font size to be smaller. So what am I doing wrong? Shouldn't android:textSize attributute work? Or is there some other way to do so? I believe that text size should be set from XML not from java as it is design related thing. Any suggestion?

Hafiz
  • 4,187
  • 12
  • 58
  • 111

7 Answers7

17

Ok, so this is my solution, you can actually use the SpannableString for fetching the text and then changing the font via RelativeSizeSpan (if you want text size relative to the default one) or via AbsoluteSizeSpan (if you want to manually input 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;
}

This example increases the size of menu item texts by 50%.

John Petrucci
  • 323
  • 3
  • 5
10

add into style xml file like this bottom code line your custom font size,,

after this

<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 have to "menu_text_style" add to your NavigationView

        app:itemTextAppearance="@style/menu_text_style"
Caner Yılmaz
  • 201
  • 3
  • 6
8

Add the "android:actionMenuTextAppearance" item for your activities in your styles.xml :

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

Apply this style to your activity in your Manifest :

<activity 
android:theme="@style/AppThemeActivity"
.../>
Pink Cocot
  • 89
  • 1
  • 2
6

So it's been a long time since this was asked but I like this solution as steven smiths answer changes every view:

Add this line to NavigationView:

app:itemTextAppearance="@style/MenuItems"

And this to the styles :

<style name="MenuItems" parent="AppTheme">
    <item name="android:textSize">18sp</item>
    <item name="android:fontFamily">sans-serif-light</item>
</style>
jobbert
  • 3,297
  • 27
  • 43
  • 1
    And that's the problem: it changes EVERY view. Most programmers want the text size to vary based on screen size/density. – FractalBob Oct 13 '17 at 19:22
  • That's what I meant, my solution Doesn't change every view. – jobbert Oct 14 '17 at 17:55
  • What I meant was that your solution isn't screen size/density independent. I need a way for the menu items to grow with the screen size. – FractalBob Oct 15 '17 at 18:08
  • @FractalBob SP and DP are screen independent. If you want to have to have a same size in percentage you should use percentages. But this isn't recommended. – jobbert Oct 15 '17 at 18:15
  • @FractalBob, what you want to do will be really difficult if sp/dp aren't doing what you want because you don't really know what size (in pixels) your object is until layout has occurred. I think most programmers want the font to appear the same physical size on the screen regardless of the pixel size/resolution of the screen it's displayed on. And that is the purpose of dp/sp. And no -- it doesn't change every text view -- it only changes those using AppTheme. You're free to define a different style and with different font size/color/typeface and apply it as you wish. – steven smith Oct 14 '18 at 20:07
  • @zeleven, please read more carefully. I said to the NavigationView, if you are to lazy to Google, here's the link: https://developer.android.com/reference/android/support/design/widget/NavigationView. Never said add it to the menu items. And yes, you'll need to use a NavigationView for this. – jobbert Apr 25 '19 at 11:07
  • Author didn't mention `NavigationView`. You can't read but you can write? – user924 Oct 06 '21 at 17:44
3

Works well, on 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"
        />
researcher
  • 1,758
  • 22
  • 25
0

Or you can set the font size in the values/styles item for the theme you are using. In my case the style indicated in the AndroidManifest.xml file is:

android:theme="@style/AppTheme"

And so my style for the AppTheme is now:

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

Seems strange it doesn't work for the item though...

steven smith
  • 1,519
  • 15
  • 31
-2
<style name="RobotoTextViewStyleEn" parent="android:Widget.TextView">
    <item name="android:textSize">15sp</item>
    <item name="android:textColor">#868784</item>
</style>

navigationView.setItemTextAppearance(R.style.RobotoTextViewStyle);
Donald Duck
  • 8,409
  • 22
  • 75
  • 99
somia molaei
  • 111
  • 1