Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 624
Michael Eilers Smith Avatar asked Oct 03 '22 15:10

Michael Eilers Smith


1 Answers

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);
}
like image 133
Jorge Gil Avatar answered Oct 29 '22 04:10

Jorge Gil