set font for text view android (from font file)

i have a font file love.ttf (5mb). i had put it into assets folder. This is my code

 @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    TextView tv=(TextView)findViewById(R.id.textView1);
    Typeface tf=Typeface.createFromAsset(getAssets(),"love.ttf");;
    tv.setTypeface(tf,Typeface.NORMAL);
    tv.setTextSize(20);
}  

main.xml files

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#4B67A1"
    android:orientation="vertical" >    
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="おはようございます。" />    
</LinearLayout>

when i run it, i can't see the text!!! how to load a large font? please help me! thanks

Upvotes: 1

Views: 2930

Answers (2)

Vipul
Vipul

Reputation: 28103

I would suggest you to not directly save font in Asset folder,rather create fonts folder under it and inside that save love.ttf font.

Then you can perform the magic as below.

  TextView tv=(TextView)findViewById(R.id.custom); 
  Typeface face=Typeface.createFromAsset(getAssets(), "fonts/love.ttf"); 
  tv.setTypeface(face); 

Upvotes: 0

Simon Dorociak
Simon Dorociak

Reputation: 33515

So, there will be firstly two possible reason why it doesn't work.

  1. Check if your font supports Japanese characters.
  2. You could have bad path of your font file.

    TextView tv = (TextView) findViewById(R.id.textView1);
    Typeface tf=Typeface.createFromAsset(getAssets(),"fonts/love.ttf");
    

You should have in your assets folder next subfolder named fonts. Then also check if you have correct suffix of font.

Upvotes: 2

Related Questions