0

I need to change the font of a TextView text. I tried to use this method but it changes the font of the whole application.

Typeface tfArial = Typeface.createFromAsset(mFragment.getContext().getAssets(), "fonts/arial.ttf");
mEmailInput.setTypeface(tfArial);

Is there a method or a way to change the font just for a word without changing the whole app font?

Thanks!

  • Is it possible to use CSS? – Bálint Budavölgyi May 03 '18 at 13:26
  • the given code will only chnage the font of emailInput view not the whole app font .. – Adeel Turk May 03 '18 at 13:36
  • Possible duplicate of [How to change fontFamily of TextView in Android](https://stackoverflow.com/questions/12128331/how-to-change-fontfamily-of-textview-in-android) – InsaneCat May 03 '18 at 13:37
  • @AdeelTurk When I use setTypeface() it changes my app font...do you know why? – Francesco Medina May 04 '18 at 07:04
  • @InsaneCat It is not a duplicate question since I asked for changing the font just for only one word in a TextView without changing the app font. I tried to use setTypeface() as indicated in the main answers but it change the whole application font. – Francesco Medina May 04 '18 at 07:05
  • @FrancescoMedina check my answer hope this may help to you – InsaneCat May 04 '18 at 07:12
  • @FrancescoMedina its not possible setTypeFace will be called against a single edit text object so it will change the font of that specifc edit text.. try something like foloowing.. Make two edi text and apply two different typefaces on each youe will see the difference in bwtween two of edit texts font.. it will not affect the font of your whole application . – Adeel Turk May 04 '18 at 10:06

3 Answers3

0

Create custom text view :

public class TextViewPlus extends TextView {
    private static final String TAG = "TextView";

    public TextViewPlus(Context context) {
        super(context);
    }

    public TextViewPlus(Context context, AttributeSet attrs) {
        super(context, attrs);
        setCustomFont(context, attrs);
    }

    public TextViewPlus(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        setCustomFont(context, attrs);
    }

    private void setCustomFont(Context ctx, AttributeSet attrs) {
        TypedArray a = ctx.obtainStyledAttributes(attrs, R.styleable.TextViewPlus);
        String customFont = a.getString(R.styleable.TextViewPlus_customFont);
        setCustomFont(ctx, customFont);
        a.recycle();
    }

    public boolean setCustomFont(Context ctx, String asset) {
        Typeface typeface = null;
        try {
            typeface = Typeface.createFromAsset(ctx.getAssets(), asset);
        } catch (Exception e) {
            Log.e(TAG, "Unable to load typeface: "+e.getMessage());
            return false;
        }

        setTypeface(typeface);
        return true;
    }
}

add xml file :

  <com.mypackage.TextViewPlus
        android:id="@+id/textViewPlus1"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:text="@string/showingOffTheNewTypeface"
        foo:customFont="my_font_name_regular.otf">
    </com.mypackage.TextViewPlus>
Kaushal Gosaliya
  • 421
  • 7
  • 16
0

in the Layout file (.xml) you need to go to the TextEdit you want to change the font of and use android:typeface or setTypeface() in Java.

Remember that Arial is not available for Android so you would need to place Arial.ttf in the assets folder (Inside assets folder create new folder named fonts and place it inside it.) Then right this in your java code:

Typeface type = Typeface.createFromAsset(getAssets(),"fonts/arial.ttf"); 
textView.setTypeface(type);
0

You can set typeface like this in three ways to specific words:

1>

String first = "This is test";
String next = "<font color='#EE0000'>Application</font>";
t.setText(Html.fromHtml(first + next));

2>

SpannableStringBuilder builder = new SpannableStringBuilder();

SpannableString str1= new SpannableString("Text1");
str1.setSpan(new ForegroundColorSpan(Color.RED), 0, str1.length(), 0);
builder.append(str1);

SpannableString str2= new SpannableString(appMode.toString());
str2.setSpan(new ForegroundColorSpan(Color.GREEN), 0, str2.length(), 0);
builder.append(str2);

TextView tv = (TextView) view.findViewById(android.R.id.text1);
tv.setText( builder, TextView.BufferType.SPANNABLE);

3>

String first = "This word is ";
String next = "red"
TextView tvText= (TextView) findViewById(R.id.textbox);

tvText.setText(first + next, BufferType.SPANNABLE);
Spannable s = (Spannable)t.getText();
int start = first.length();
int end = start + next.length();
s.setSpan(new ForegroundColorSpan(0xFFFF0000), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

Here in 3rd solution you also set a starting and ending position to set your typeface or spannable string.

Hope this helps to you.

InsaneCat
  • 2,115
  • 5
  • 21
  • 40