Changing text Size of menu item in android

61,454

Solution 1

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%.

Solution 2

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"

Solution 3

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"
.../>

Solution 4

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>

Solution 5

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"
        />
Share:
61,454

Related videos on Youtube

Hafiz
Author by

Hafiz

I have 8 years of experience Developing web based applications. Have experience in managing short teams as well as working in teams. Most of my technical experience is in but not limited to PHP, MySQL, Python and JavaScript(mostly client side but also have used node.js) but I am language agnostic and always open to learn more. I have worked on both frontend and backend in different projects with different responsibilities. I have created teams as well as managed teams. Book: I have written "Building RESTful Webservices in PHP7" with Packt pub. https://www.packtpub.com/application-development/building-restful-web-services-php-7 Skillset: I am listing my skillset here so that you can look at it if required: Backend: Mostly used PHP and Python on server side. PHP Frameworks Used: Laravel, Lumen, Kohana, Code Igniter Python Frameworks used: Django, Scrapy Good understanding and experience in creating and consuming REST API Used Codeception for REST API Testing Storage/DB: Used MySQL in most of projects while PgSQL and elasticsearch both are used in one project only. Basic understanding of MongoDB and have developed a basic todo app in Laravel with MongoDB. Also supervised a team which used Neo4j, so have high level knowledge of that as well. LAMP Server Configuration: Can and have configured LAMP server on Amazon EC2 as well as DigitalOcean. Also configured mod_wsgi for Django with Apache2. Frontend: Good at HTML5 and CSS Basic knowledge of Phonegap, bootsrap, HTML5 Canvas, SVG, Photoshop and Web Sockets Datavisualization using D3 that renders SVG. version control system: SVN, Git Mobile: Basic understanding and working knowledge of Android SDK including some of its UI components, SQLite and AsyncTask as I used them in my BS project. Operating Systems: Ubuntu and Windows Other than above skill-set I am good learner and always ready to learn anything required to accomplish work. Other interests: I am always interested in improving things.Other than what I have currently done and know. I am also interested in working in Node JS, functional paradigm, WebGL, noSQL and search engines. Detail: I have done many projects which are different in nature than each other. Some of them are listed in my resume with my experience and other detail. However, please note that my bigger plus is not my skill-set but my quick learning abilities.

Updated on June 14, 2021

Comments

  • Hafiz
    Hafiz about 3 years

    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?

    • Salman Khakwani
      Salman Khakwani almost 11 years
      have you tried using dp or px in your android:textSize="" tag ?
    • Hafiz
      Hafiz almost 11 years
      @SalmanKhakwani : tried but that didn't work
  • Mani
    Mani over 9 years
    It affects the whole applications textviews:(
  • FractalBob
    FractalBob over 6 years
    And that's the problem: it changes EVERY view. Most programmers want the text size to vary based on screen size/density.
  • jobbert
    jobbert over 6 years
    That's what I meant, my solution Doesn't change every view.
  • FractalBob
    FractalBob over 6 years
    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.
  • jobbert
    jobbert over 6 years
    @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.
  • steven smith
    steven smith over 5 years
    @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.
  • jobbert
    jobbert about 5 years
    @zeleven, please read more carefully. I said to the NavigationView, if you are to lazy to Google, here's the link: developer.android.com/reference/android/support/design/widge‌​t/…. Never said add it to the menu items. And yes, you'll need to use a NavigationView for this.
  • Andrew
    Andrew almost 3 years
    There is no "itemTextAppearance" anymore, just "itemTextAppearanceActive" and inactive
  • user924
    user924 over 2 years
    Author didn't mention NavigationView. You can't read but you can write?
  • user924
    user924 over 2 years
    Author didn't mention NavigationView. You can't read but you can write?