0

hi there i am newer in android and i want to ask how to use one font for many text views i have use this method

    final String DosisPath = "fonts/Dosis-Bold.ttf";
    final Typeface Dosis_Bold = Typeface.createFromAsset(getAssets(), DosisPath);
    TextView txtCancle = (TextView) findViewById(R.id.cancle);
    txtCancle.setTypeface(Dosis_Bold);

and it's work good but i try to make the same Typeface for another TextView but that dosen't work like this

 final String DosisPath = "fonts/Dosis-Bold.ttf";
    final Typeface Dosis_Bold = Typeface.createFromAsset(getAssets(), DosisPath);
    TextView txtCancle = (TextView) findViewById(R.id.cancle);
    txtCancle.setTypeface(Dosis_Bold);
    EditText ETCode  = (EditText)findViewById(R.id.secET);
    ETCode.setTypeface(Dosis_Bold);

if the is an error i have done tell me and if there is another way to make that tell me too thanks alot .

Wesam Torman
  • 288
  • 2
  • 9

1 Answers1

1

This is how you should do it.

Create a class

public class TextViewContent extends TextView {

    public TextViewContent(Context context) {
        super(context);
        setCustomFont(context);
    }

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

    public TextViewContent(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);

        if (isInEditMode()) {
            return;
        }
        setCustomFont(context);

    }

    private void setCustomFont(Context ctx) {
        setTypeface(Typeface.createFromAsset(ctx.getAssets(),
                "Champagne_Limousines.ttf"));
    }
}

and in your layout file

<com.hardik.test.widget.TextViewContent
            style="@style/TextMedium"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="5dip"
            android:layout_marginTop="5dip"
            android:gravity="center"
            android:text="@string/tag_line" />

This will display textview in chosen font. Same way for EditText you can extends the EditText class and rest is same.

Hardik Trivedi
  • 5,677
  • 5
  • 31
  • 51