Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing text Size of menu item in android

Tags:

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?

like image 784
Hafiz Avatar asked Aug 18 '13 16:08

Hafiz


People also ask

What is the default text size in Android Studio?

The body text size in Material Design is 14sp. You should think of this as the normal font size, and basically everything else a variation on it. For instance, while 14sp is the default text size when the text can be quite long, when there's only a small modal with a bit of text, it's 16sp!


2 Answers

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

like image 178
John Petrucci Avatar answered Sep 23 '22 14:09

John Petrucci


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" 
like image 43
Caner Yılmaz Avatar answered Sep 23 '22 14:09

Caner Yılmaz