Sunil_Suthar
Sunil_Suthar

Reputation: 1094

How to make a custom TextView?

I'm trying to make a custom text view that has the font set from a given path. Please provide me any example and how I can make that with less code:

<TextView
   android:id="@+id/textView2"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="@string/accountInfoText"
   android:textColor="#727272"
   android:textSize="18dp" />

Upvotes: 41

Views: 102523

Answers (3)

bond007
bond007

Reputation: 2494

Create a Custom View for Textview.

  1. Make the entry in the attrs.xml file and give an option to select Font as a list in custom TextView.

    ....
  2. Create the enum entry with a list of fonts and assign unique values

  3. Make entries of all fonts in strings.xml

    Roboto-Bold Roboto-Medium Roboto-Light Roboto-Regular Roboto-Thin Roboto-Italic

  4. Create an asset folder and copy all the necessary font you want to put in the font folder

  5. Create a class extending TextView

    import android.content.Context;
    import android.content.res.TypedArray;
    import android.graphics.Typeface;
    import android.util.AttributeSet;
    import android.widget.TextView;
    
    /**
    * Created by ANKIT 
    */
    public class CustomFontTextView extends TextView {
    
    String customFont;
    
    public CustomFontTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        style(context, attrs);
    }
    
    public CustomFontTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        style(context, attrs);
    
    }
    
    private void style(Context context, AttributeSet attrs) {
    
        TypedArray a = context.obtainStyledAttributes(attrs,
                R.styleable.CustomFontTextView);
        int cf = a.getInteger(R.styleable.CustomFontTextView_fontName, 0);
        int fontName = 0;
        switch (cf)
        {
            case 1:
                fontName = R.string.Roboto_Bold;
                break;
            case 2:
                fontName = R.string.Roboto_Italic;
                break;
            case 3:
                fontName = R.string.Roboto_Light;
                break;
            case 4:
                fontName = R.string.Roboto_Medium;
                break;
            case 5:
                fontName = R.string.Roboto_Regular;
                break;
            case 6:
                fontName = R.string.Roboto_Thin;
                break;
            default:
                fontName = R.string.Roboto_Regular;
                break;
        }
    
        customFont = getResources().getString(fontName);
    
        Typeface tf = Typeface.createFromAsset(context.getAssets(),
                "font/" + customFont + ".ttf");
        setTypeface(tf);
        a.recycle();
    }
    }
    

You can use this custom class this way. .. use your packageName.ClassName

 <ankit.com.customui.CustomFontTextView
  android:layout_width="match_parent"
  android:text="Hello World Ankit"
  android:textSize="16sp"
  app:fontName="Roboto_Medium"
  android:layout_height="wrap_content"/>

add below styleable in res/values/attrs.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="CustomFontTextView">
        <attr name="fontName" format="string" />
    </declare-styleable>
</resources>

Upvotes: 49

Chitranshi Srivastava
Chitranshi Srivastava

Reputation: 31

If you're writing in Kotlin, this is how you can do it. First of all, add your font to the font folder. Then, create a separate Kotlin class. Call it something like HeadlineTextView (or whatever that sort of gives a hint about what the TextView is for).

class HeadlineTextView(context: Context, attributeSet: AttributeSet) : AppCompatTextView(context, attributeSet) {

  init {
      applyFont()
  }

  private fun applyFont() {
    val headlineTypeface: Typeface=Typeface.create("Montserrat", Typeface.NORMAL)
    typeface=headlineTypeface
  }

}

Now use this custom Text View in your XML. Replace this -

<TextView
   android:id="@+id/textView2"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="@string/accountInfoText"
   android:textColor="#727272"
   android:textSize="18dp" />

With this -

<com.gmail.something.myapp.HeadlineTextView
   android:id="@+id/textView2"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="@string/accountInfoText"
   android:textColor="#727272"
   android:textSize="18dp" />

Upvotes: 0

SuN
SuN

Reputation: 1743

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.widget.TextView;

public class FontTextView extends TextView {


    public FontTextView(Context context) {
      super(context);
      Typeface face=Typeface.createFromAsset(context.getAssets(), "Helvetica_Neue.ttf"); 
      this.setTypeface(face); 
    }

    public FontTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
     Typeface face=Typeface.createFromAsset(context.getAssets(), "Helvetica_Neue.ttf"); 
  this.setTypeface(face); 
    }

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

    protected void onDraw (Canvas canvas) {
        super.onDraw(canvas);
        
       
    }

}

and in xml:

<com.util.FontTextView
                    android:id="@+id/textView2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/accountInfoText"
                    android:textColor="#727272"
                    android:textSize="18dp" />

Upvotes: 102

Related Questions