How to dynamically change option menu item text font in Android

In this android programming source code example, we are going to dynamically change option menu item text font in Android.

You can copy and adopt this source code example to your android project without reinventing the wheel.

Below is a step by step source code to dynamically change option menu item text font in Android.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ededed">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="Option Menu"
        android:gravity="center"
        android:textSize="20sp"/>

</RelativeLayout>

MainActivity.java

import android.content.Context;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.style.TypefaceSpan;
import android.util.AttributeSet;
import android.view.InflateException;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.SubMenu;
import android.view.View;
import android.widget.TextView;



public class MenuActivity8 extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_menu8);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.option_menu, menu);

        Typeface typeface = Typeface.createFromAsset(getAssets(), "Font.otf");

        for(int i = 0; i<menu.size(); i++){
            MenuItem menuItem = menu.getItem(i);
            SubMenu subMenu = menuItem.getSubMenu();
            if(subMenu != null && subMenu.size()>0){
                for(int j = 0; j < subMenu.size(); j++){
                    MenuItem subMenuItem = subMenu.getItem(j);
                    applyFont(subMenuItem, typeface);
                }
            }
            applyFont(menuItem, typeface);
        }
        return super.onCreateOptionsMenu(menu);
    }

    private void applyFont(MenuItem menuItem, Typeface font){
        SpannableString spannableString  = new SpannableString(menuItem.getTitle());
        spannableString.setSpan(new CustomTypeFace("", font), 0, spannableString.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        menuItem.setTitle(spannableString);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        super.onOptionsItemSelected(item);
        switch(item.getItemId()){
            //
        }

        return true;
    }

    private class CustomTypeFace extends TypefaceSpan{
        private final Typeface typeface;

        public CustomTypeFace(String family, Typeface type) {
            super(family);
            typeface = type;
        }

        @Override
        public void updateDrawState(TextPaint textPaint){
            applyCustomTypeFace(textPaint, typeface);
        }

        @Override
        public void updateMeasureState(TextPaint textPaint){
            applyCustomTypeFace(textPaint, typeface);
        }

        private void applyCustomTypeFace(Paint paint, Typeface typeface){
            int oldStyle;
            Typeface old = paint.getTypeface();
            if(old == null){
                oldStyle = 0;
            }else{
                oldStyle = old.getStyle();
            }

            int fake = oldStyle & ~typeface.getStyle();
            if((fake & Typeface.BOLD) != 0){
                paint.setFakeBoldText(true);
            }

            if((fake & Typeface.ITALIC) != 0){
                paint.setTextSkewX(-0.25f);
            }
            paint.setTypeface(typeface);
        }
    }
}

menu\option_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto">

    <item
        android:id="@+id/action_home"
        android:title="@string/home"
        android:orderInCategory="100"
        app:showAsAction="never" />

    <item
        android:id="@+id/action_blog"
        android:title="@string/blog"
        android:orderInCategory="100"
        app:showAsAction="never" />

    <item
        android:id="@+id/action_App"
        android:title="@string/App"
        android:orderInCategory="100"
        app:showAsAction="never" />
</menu>

If you have any question or suggestions kindly use the comment box or you can contact us directly through our contact page below.

 

Add a Comment