Customization of the Menu

Asked

Viewed 33 times

-1

I want to change the font and color of the items and also change the background of the menu, but I cannot do this in the Xml menu. Screenshot below:

inserir a descrição da imagem aqui

I also want to make Expense subthemes appear just below Expense. It opens a new menu, making the previous one disappear (obvious).

XML menu:

<?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/mnDespesas"
        android:title="Despesas"
        app:showAsAction="never">
        <menu>
            //esconder subHab dentro do group
            <item
                android:id="@+id/subHab"
                android:title="Habitação"
                app:showAsAction="never">
            </item>
            <item
                android:id="@+id/subSaud"
                android:title="Saúde"
                app:showAsAction="never">
            </item>
            <item
                android:id="@+id/subDespPess"
                android:title="Despesas Pessoais"
                app:showAsAction="never">
            </item>
            <item
                android:id="@+id/subEdu"
                android:title="Educação"
                app:showAsAction="never">
            </item>
            <item
                android:id="@+id/subLaz"
                android:title="Lazer"
                app:showAsAction="never">
            </item>
            <item
                android:id="@+id/subOutr"
                android:title="Outros"
                app:showAsAction="never">
            </item>
        </menu>
    </item>
    <item
        android:id="@+id/mnDivida"

        android:title="Organizador de Dívidas"
        app:showAsAction="never">
    </item>
    <item
        android:id="@+id/mnAClen"
        android:title="Calendário"
        app:showAsAction="never">
    </item>
    <item
        android:id="@+id/mnRanking"
        android:title="Ranking"
        app:showAsAction="never">
    </item>
    <item
        android:id="@+id/mnAjuda"
        android:title="Ajuda"
        app:showAsAction="never">
    </item>



</menu>

1 answer

0

To exchange the font add the following class to your project:

import android.graphics.Paint;
import android.graphics.Typeface;
import android.text.TextPaint;
import android.text.style.TypefaceSpan;

public class CustomTypefaceSpan extends TypefaceSpan {

    private final Typeface newType;

    public CustomTypefaceSpan(String family, Typeface type) {
        super(family);
        newType = type;
    }

    @Override
    public void updateDrawState(TextPaint ds) {
        applyCustomTypeFace(ds, newType);
    }

    @Override
    public void updateMeasureState(TextPaint paint) {
        applyCustomTypeFace(paint, newType);
    }

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

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

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

        paint.setTypeface(tf);
    }
}

In the class containing your use menu (change as needed) the following methods:

    Menu m = nv.getMenu();
    for (int i = 0; i < m.size(); i++) {
        MenuItem mi = m.getItem(i);

        SubMenu subMenu = mi.getSubMenu();
        if (subMenu != null && subMenu.size() > 0) {
            for (int j=0; j <subMenu.size();j++) {
                MenuItem subMenuItem = subMenu.getItem(j);
                applyFontToMenuItem(subMenuItem);
            }
        }
        applyFontToMenuItem(mi);
    }

and

    private void applyFontToMenuItem(MenuItem mi) {
        Typeface univia = Typeface.createFromAsset(getAssets(), "fonts/univia.otf");
        SpannableString item = new SpannableString(mi.getTitle());
        item.setSpan(new CustomTypefaceSpan("" , univia), 0 , item.length(),  Spannable.SPAN_INCLUSIVE_INCLUSIVE);
        mi.setTitle(item);
    }

Finally, don’t forget that you need to have the /Assets/fonts/ folder (at the same level as /java and /res) and put your font there. Good luck!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.