How to change the Menu text size

15,256

Solution 1

In your application's base theme, add the following item:

<item name="android:actionMenuTextAppearance">@style/customActionBar</item>

Then define customActionBar as follows:

<style name="customActionBar" parent="@android:style/TextAppearance.Holo.Widget.ActionBar.Menu">
    <item name="android:textSize">16sp</item>
</style>

Solution 2

Here you go, you should fetch the text from MenuItems to SpannableStrings in your onCreateOptionsMenu, change the text size compared to the default one via RelativeSizeSpan. If you want to input absolute font size use AbsoluteSizeSpan, e.g. to 18 dpi = AbsoluteSizeSpan(18,true):

public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);
    MenuInflater infl = getMenuInflater();
    infl.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;
}
Share:
15,256
Admin
Author by

Admin

Updated on July 25, 2022

Comments

  • Admin
    Admin almost 2 years

    I had tried change the style like this:

    <item name="android:actionMenuTextAppearance">@style/MenuTextStyle</item>
    <stylename="MenuTextStyle"parent="@style/TextAppearance.AppCompat.Widget.ActionBar.Menu">
            <item name="android:textColor">@android:color/white</item>
            <item name="android:textSize">16sp</item>
    </style>
    

    But it did work. sorry for my english. If you know it, tell me please. Thank you in advance!

  • Admin
    Admin about 9 years
    I use toolbar in my application so cannot user the style like xxx.ActionBar.menu.
  • John Petrucci
    John Petrucci over 8 years
    Explanations are given now.