0

I want to using other font on the textview. I already Created custom textview class and created fonts folder.The problem is the font not changed after compiled.I dont know what mistake that i make.I hope you guys can help me solve this problem.Thank you in advance

enter image description here

enter image description here

activity_example_one.xml

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.khoi.androidexample.example.Example_One_Activity"> <com.khoi.androidexample.custom.TextViewBold android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/texview_bold_id" android:text="Hello World!"/> <com.khoi.androidexample.custom.TextViewMedium android:layout_width="match_parent" android:id="@+id/textview_medium_id" android:layout_height="wrap_content" android:text="Hello World!"/> </LinearLayout> 

Example_One_Activity.java

public class Example_One_Activity extends AppCompatActivity { TextViewBold textViewBold; TextViewMedium textViewMedium; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_example_one); textViewBold = (TextViewBold) findViewById(R.id.texview_bold_id); textViewMedium = (TextViewMedium) findViewById(R.id.textview_medium_id); } } 

TextViewBold.java

public class TextViewBold extends TextView {

public TextViewBold(Context context, AttributeSet attrs, int defStyle){ super(context, attrs, defStyle); init(context); } public TextViewBold(Context context,AttributeSet attrs){ super(context,attrs); init(context); } public TextViewBold(Context context){ super(context); init(context); } private void init(Context context) { if (!isInEditMode()) { Typeface tf = Typeface.createFromAsset(context.getAssets(),"Lato_Medium.ttf"); setTypeface(tf); } } 

}

3 Answers 3

1

Pass context in your init(Context context)

And getAssets() like --> context.getAssets()

Typeface tf = Typeface.createFromAsset(context.getAssets(),"Lato_Medium.ttf"); 

isInEditMode() -->Ensure it will return true

Sign up to request clarification or add additional context in comments.

6 Comments

I already add context as parameter and !isInEditMode() retun true, but textview font still not changed.
pls check once again without if(! isInEditMode()) and update me soon
if isInEditMode() only its return false.
Hold the ctrl key & move your mouse cursor into "Lato_Medium.ttf" if you click that text it will open the file or not? Pls confirm me I will give the solution.
Your code looks right. No problem in code. Check your ttf file. Check same code with other font file. Please update your question code. Because you use wrong one - getResources().getAssets(). So please update your question code
|
1

You are missing "fonts/" from your address , it should be like this :

 setTypeface(Typeface.createFromAsset(getContext().getAssets(),"fonts/Lato_Medium.ttf")); 

2 Comments

I got an error message : Caused by: java.lang.RuntimeException: Font asset not found fonts/Lato_Medium.ttf
try rebuild the project , then copy the exact code i edited above , it should works, i cant see any problem in the code
0

I re-download font style and replace the file inside fonts folder and it worked.Thank you very much for helping solved this problem

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.