3

I have a question regarding adding multiple custom fonts to textview. I have basically added the fonts in fonts folder and have created a java class for fonttextview based on the solutions i found online. However I see they have added only one font and I want to add multiple fonts like roboto-regular,roboto-bold,cabin-bold etc. Here's the code I have so far:

public class FontTextView extends TextView {


    public FontTextView(Context context) {
      super(context);
      Typeface face=Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-Bold.ttf"); 
      this.setTypeface(face);

    }

    public FontTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
     Typeface face=Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-Bold.ttf"); 
  this.setTypeface(face); 
    }

    public FontTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
     Typeface face=Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-Bold.ttf"); 
  this.setTypeface(face); 
    }

How do I go about it creating multiple fonts? Also, I tried the styleable etc, but it shows error as it doesnt support styleable class, can anyone add another font to this existing code and walk me through the retrieval process?

Thanks! Justin

  • You want to use different fonts at once?? – AnujMathur_07 May 30 '13 at 13:29
  • i want to use some if else construct to use a particular font but define it in the same class. Also,I ant to call the particulat font in the if else construct throuch an "id" in my xml file –  May 30 '13 at 13:42
  • I have posted an answer, I think that is what you are asking. – AnujMathur_07 May 30 '13 at 13:44
  • thanks, quick question, can you let me know how to fix the styleable issue and where to include the xlmns when you mention main and for the resources file added attrs.xml do i have to specifically have to add the font names and their type of generic one as you have shown? "string" –  May 30 '13 at 14:04
  • xlmns would be added in the top layout where there is `xmlns:android="http://schemas.android.com/apk/res/android"`, just after that. – AnujMathur_07 May 30 '13 at 14:08
  • and for the fonts, you have to add them in the `assets/fonts/` and add which font you need in the xml file. Like for `roboto-regular.ttf` use `your_name:customFont="roboto-regular.ttf" – AnujMathur_07 May 30 '13 at 14:12

3 Answers3

5

Use the following code for different fonts set to the xml file.

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

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

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

public CustomTextView(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.CustomTextView);
    String customFont = a.getString(R.styleable.CustomTextView_customFont);
    setCustomFont(ctx, customFont);
    a.recycle();
}

public boolean setCustomFont(Context ctx, String asset) {
    Typeface tf = null;
    try {
    tf = Typeface.createFromAsset(ctx.getAssets(), "fonts/"+asset);  
    } catch (Exception e) {
        Log.e(TAG, "Could not get typeface: "+e.getMessage());
        return false;
    }

    setTypeface(tf);  
    return true;
}

}

And in the xml file you can use it as:

<com.package_name.CustomTextView
           your_name:customFont="arialbd.ttf" />

and int the main parent layout add

xmlns:your_name="http://schemas.android.com/apk/res/com.package_name"

and remember to add a attrs.xml in values folder, with following resource in it

<resources>
<declare-styleable name="CustomTextView">
    <attr name="customFont" format="string"/>
</declare-styleable>

Hope it helps

AnujMathur_07
  • 2,586
  • 2
  • 18
  • 25
  • thanks for the help.I added the above code, however I see an error with styleable, which shows the following upon clicking"quickfix" :Link all references for a local rename (does not change references in other files), where it is asking me to rename it. How do I go about it? –  May 30 '13 at 13:54
  • Try cleaning your project. – AnujMathur_07 May 30 '13 at 14:06
  • no,I am getting an error in the customtextview where it is not supporting styleable. –  May 30 '13 at 14:08
  • If there is an Import `import android.R.*;`, remove it. – AnujMathur_07 May 30 '13 at 14:13
  • There is only: import android.R; which I think is needed, as soon as I remove that, it shows an error asking to import R –  May 30 '13 at 14:16
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/30915/discussion-between-justice-bauer-and-anujmathur-07) –  May 30 '13 at 14:22
2

I suggest to use HTML in your text so you can use different color/type face/...

Have a look at:

Html in text view with different fonts for bold and italic

Is it possible to have multiple styles inside a TextView?

Using size HTML attribute in TextView

A interesting solution is coding a Typeface span:

 public class CustomTypefaceSpan extends TypefaceSpan {

look at How can I use TypefaceSpan or StyleSpan with a custom Typeface?

Community
  • 1
  • 1
Seraphim's
  • 12,559
  • 20
  • 88
  • 129
  • that doesnt help? I want to set custom fonts in my existing code,for example: Typeface face=Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-Bold.ttf"); this.setTypeface(face); Typeface face1=Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-Medium.ttf"); this.setTypeface(face1); with some if else loop and an id to call it through my xml file? how do i go about it ? any clue? –  May 30 '13 at 13:41
  • If you intend to use multiple typeface on the same TextView look also at: http://stackoverflow.com/questions/4819049/how-can-i-use-typefacespan-or-stylespan-with-custom-typeface – Seraphim's May 30 '13 at 13:43
1

You can write html style in text and use

textView.setText(Html.fromHtml(displayString));

sample string to add color

String displayString = " <p style=\"color:#B4009E;\">Your string </p>" ;

This is how we can do html styles in textView

Jithu
  • 1,478
  • 1
  • 13
  • 21