chrisjenx / Calligraphy

Custom fonts in Android the easy way...
Apache License 2.0
8.59k stars 1.09k forks source link

Is there any way to use custom fonts in Menu Item? #82

Open stepango opened 10 years ago

chrisjenx commented 10 years ago

MenuItems are added dynamically. We are looking at adding support for this. But might be during v2.0 rewrite where we need to traverse none inflated views.

dimskom commented 9 years ago

I think the only problem with Action bar menu items is in the "com.android.internal.view.menu.ActionMenuItemView" class as toolbar works.

My solution is (bit on the ugly side)

in the CalligraphyFactory class add method

protected static boolean isActionMenuItemView(View view)
    {
        Class<?> aClass = null;
        try
        {
            aClass = Class.forName("com.android.internal.view.menu.ActionMenuItemView");
        }
        catch (ClassNotFoundException e)
        {
            return false;
        }
        return view.getClass().equals(aClass);
    }

and then use this like the work around for the action bar in onViewCreatedInternal method

final boolean deferred = matchesResourceIdName(view, ACTION_BAR_TITLE) || matchesResourceIdName(view, ACTION_BAR_SUBTITLE) || isActionMenuItemView(view);

Maybe you can improve on this solution and add it to your next release

chrisjenx commented 9 years ago

Interesting I guess that could work. I'm not sure of the over head implications of that.

On Mon, 4 May 2015 11:04 dimskom notifications@github.com wrote:

I think the only problem with Action bar menu items is in the "com.android.internal.view.menu.ActionMenuItemView" class as toolbar works.

My solution is (bit on the ugly side)

in the CalligraphyFactory class add method

protected static boolean isActionMenuItemView(View view) { Class<?> aClass = null; try { aClass = Class.forName("com.android.internal.view.menu.ActionMenuItemView"); } catch (ClassNotFoundException e) { return false; }

return view.getClass().equals(aClass);

}

and then use this like the work around for the action bar in onViewCreatedInternal method

final boolean deferred = matchesResourceIdName(view, ACTION_BAR_TITLE) || matchesResourceIdName(view, ACTION_BAR_SUBTITLE) || isActionMenuItemView(view);

Maybe you can improve on this solution and add it to your next release

— Reply to this email directly or view it on GitHub https://github.com/chrisjenx/Calligraphy/issues/82#issuecomment-98660915 .

dimskom commented 9 years ago

My solution worked nicely, maybe you can build on it for your next version. Great project !

chrisjenx commented 9 years ago

@dimskom Glad that it works. I guess, I would need to lazily init Class.forName() as thats not exactly performant whilst iterating through inflating views.

dimskom commented 9 years ago

here is more efficient and robust solution (sorry about the method, member names)

CalligraphyFactory.java

    private static final String[] msSpecialClassesNames = new String[]{"com.android.internal.view.menu.ActionMenuItemView"};
    private static HashMap<String, Class<?>> msSpecialClasses;

    static
    {
        msSpecialClasses = new HashMap<>();
        Class<?> aClass = null;
        for (int i = 0; i < msSpecialClassesNames.length; i++)
        {
            try
            {
                aClass = Class.forName(msSpecialClassesNames[i]);
                msSpecialClasses.put(msSpecialClassesNames[i], aClass);
            }
            catch (ClassNotFoundException e)
            {
            }

        }
    }

then add this method

    protected static boolean shouldDeferView(View view)
    {
        return msSpecialClasses.containsKey(view.getClass());
    }

then use it

final boolean deferred = matchesResourceIdName(view, ACTION_BAR_TITLE) || matchesResourceIdName(view, ACTION_BAR_SUBTITLE) || shouldDeferView(view);
berendn commented 9 years ago

Will this be supported in the near future?

chrisjenx commented 9 years ago

@berendn if I can convince Google to inflate the views then that would be preferred. I don't want to be supporting loads of random hacks to do this.