1

Assume my title is "Nearby - Friends" I want to show that as "Nearby- Friends"

How can I achieve it.

I tired a lot but I can't achieve that.

IMAGE REFERENCE

HERE is code that I use, I use Typeface

TextView text = (TextView) tab.getCustomView();
String subTitle = text.getText().toString().trim();
text.setTextColor(getResources().getColor(R.color.white));
Typeface face = Typeface.createFromAsset(getAssets(), "fonts/Roboto-Bold.ttf");
text.setTypeface(face);
TextView title = new TextView(getApplicationContext());
title.setText("Nearby - ");
title.setTypeface(face);
if (subTitle.equals("NEAR BY")) {
    subTitle = "People";
} else if (subTitle.equals("FRIENDS")) {
    subTitle = "Friends";
} else if (subTitle.equals("FAMILY")) {
    subTitle = "Family";
}
toolbar.setTitle(title.getText().toString() + subTitle);
Andi
  • 119
  • 2
  • 8

3 Answers3

3

Try This

Let say myToolBar is your toolbar layout in xml.

Toolbar toolBar = (Toolbar) findViewById(R.id.myToolbar);
setSupportActionBar(toolBar);

Then Add Below code

String title = "<b>Near by-</b>Friends"
getSupportActionBar().setTitle(Html.fromHtml(title));
Abhishek
  • 3,348
  • 3
  • 15
  • 34
0

You can use SpannableString to set different font style for single string sentence in TextView or other text enabled component

String strMessage="Nearby- Friends";
Spannable spannable = new SpannableString(strMessage);

//"Nearby-" bold font
spannable.setSpan(getTextAppearanceSpan(ContextCompat.getColor(mContext, android.R.color.BLACK),Typeface.BOLD), 0, 7, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    textView.setText(spannable);


//"Friends" normal font
spannable.setSpan(getTextAppearanceSpan(ContextCompat.getColor(mContext, android.R.color.BLACK),Typeface.NORMAL), 8, strMessage.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    textView.setText(spannable);

TextAppearanceSpan method to customized text with color

/*
 *get customized text with color, style
 */
private TextAppearanceSpan getTextAppearanceSpan(int color, int fontStyle) {
    ColorStateList blueColor1 = new ColorStateList(new int[][]{new int[]{}}, new int[]{color});
    return new TextAppearanceSpan(null, fontStyle, -1, blueColor1, null);
}
Kishore Jethava
  • 6,666
  • 5
  • 35
  • 51
0

If you want use two different fonts in TextView (e.g. toolbar title) you should use Spannable to apply font to each part of text.

The following solution will apply two custom fonts from your resources.

String title = "Nearby- ";
String subtitle = "Friends";
Typeface typefaceBold =  Typeface.createFromAsset(getAssets(), "fonts/Roboto-Bold.ttf");
Typeface typefaceNormal =  Typeface.createFromAsset(getAssets(), "fonts/Roboto.ttf");
SpannableString spannableTitle = new SpannableString(title + subtitle);
spannableTitle.setSpan (new BetterTypefaceSpan(typefaceBold), 0, title.length(), Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
spannableTitle.setSpan (new BetterTypefaceSpan(typefaceNormal), title.length(), spannableTitle.length(), Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
toolbar.setText(spannableTitle);

BetterTypefaceSpan.java

// Copy of CalligraphyTypefaceSpan
// https://github.com/chrisjenx/Calligraphy/blob/df1c07f82926831a5c47443bc49f9ff718a4c700/calligraphy/src/main/java/uk/co/chrisjenx/calligraphy/CalligraphyTypefaceSpan.java

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

public class BetterTypefaceSpan extends MetricAffectingSpan {
    private final Typeface typeface;

    public BetterTypefaceSpan(final Typeface typeface) {
        if (typeface == null) {
            throw new IllegalArgumentException("typeface is null");
        }

        this.typeface = typeface;
    }

    @Override
    public void updateDrawState(final TextPaint drawState) {
        apply(drawState);
    }

    @Override
    public void updateMeasureState(final TextPaint paint) {
        apply(paint);
    }

    private void apply(final Paint paint) {
        final Typeface oldTypeface = paint.getTypeface();
        final int oldStyle = oldTypeface != null ? oldTypeface.getStyle() : 0;
        final int fakeStyle = oldStyle & ~typeface.getStyle();

        if ((fakeStyle & Typeface.BOLD) != 0) {
            paint.setFakeBoldText(true);
        }

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

        paint.setTypeface(typeface);
    }
}