Search code examples
androidactionbarsherlock

How to set custom font to sherlock's contextual action bar?


I know how to set a custom font to the action bar. I just have to extend SherlockFragmentActivity and override setTitle like this:

@Override
public void setTitle(CharSequence title) {
    String str = String.valueOf(title);
    str = str.toUpperCase(Locale.getDefault());
    SpannableString s = new SpannableString(str);
    MetricAffectingSpan span = new MetricAffectingSpan() {
        @Override
        public void updateMeasureState(TextPaint p) {
            p.setTypeface(FontManager.INSTANCE.getAppFont());
        }

        @Override
        public void updateDrawState(TextPaint tp) {
            tp.setTypeface(FontManager.INSTANCE.getAppFont());
        }
    };

    s.setSpan(span, 0, s.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

    getSupportActionBar().setTitle(s);
}

However, things get complicated with a contextual action bar. The library uses a factory to return the contextual action bar, like this:

ActionMode mode = getSherlockActivity().startActionMode(mActionModeCallback);
mode.setTitle("whatever");

I COULD override ActionMode, yet the lib won't return it.

Any ideas?


Solution

  • I see is a little bit complex...

    You will need to create a View where you will place your TextView as the title, set the font you want for this TextView, and use setCustomView to place your view with the new font.

    I hope it helps you.

    UPDATE

    Have you tried create your own method, like this:

    public void setActionModeTitle(CharSequence title) {
        String str = String.valueOf(title);
        str = str.toUpperCase(Locale.getDefault());
        SpannableString s = new SpannableString(str);
        MetricAffectingSpan span = new MetricAffectingSpan() {
            @Override
            public void updateMeasureState(TextPaint p) {
                p.setTypeface(FontManager.INSTANCE.getAppFont());
            }
    
            @Override
            public void updateDrawState(TextPaint tp) {
                tp.setTypeface(FontManager.INSTANCE.getAppFont());
            }
        };
    
        s.setSpan(span, 0, s.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    
        actionMode.setTitle(s);
    }