Reputation: 63
I am making android app that will have to show text by "NASTALIQ.TTF" font. I placed the font ttf file in following directories of my project:
C:\Users\Zeeshan\AndroidStudioProjects\NasirKazmiPehliBarish\app\build\intermediates\assets\fonts\NASTALIQ.TTF
C:\Users\Zeeshan\AndroidStudioProjects\NasirKazmiPehliBarish\app\build\generated\assets\fonts\NASTALIQ.TTF
I used following line to load font:
Typeface tf = Typeface.createFromAsset(this.getAssets(), "/fonts/NASTALIQ.TTF");
but it throws exception that font not found. Here is the logcat:
09-24 20:42:59.997 2380-2380/com.neatsoft.nasirkazmipehlibarish E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.neatsoft.nasirkazmipehlibarish, PID: 2380
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.neatsoft.nasirkazmipehlibarish/com.neatsoft.nasirkazmipehlibarish.Ghazals}: java.lang.RuntimeException: Font asset not found /fonts/NASTALIQ.TTF
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by: java.lang.RuntimeException: Font asset not found /fonts/NASTALIQ.TTF
at android.graphics.Typeface.createFromAsset(Typeface.java:206)
at com.neatsoft.nasirkazmipehlibarish.Ghazals.onCreate(Ghazals.java:81)
at android.app.Activity.performCreate(Activity.java:6679)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Following is my Ghazals.java that contain the font code:
package com.neatsoft.nasirkazmipehlibarish;
import android.content.Intent;
import android.graphics.Typeface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
import java.io.File;
import static android.R.attr.typeface;
public class Ghazals extends AppCompatActivity {
static int GHAZAL_POSITION=0;
TextView tvGhazals;
String ghazals[] = {"میں نے جب لکھنا سیکھا تھا\n" +
"پہلے تیرا نام لکھا تھا\n" +
"\n" +
"میں وہ صبرِ صمیم ہوں جس نے\n" +
"بار امانت سر پہ لیا تھا\n" +
"\n" +
"میں وہ اسمِ عظیم ہوں جس کو\n" +
"جن و ملک نے سجدہ کیا تھا\n" +
"\n" +
"تو نے کیوں مرا ہاتھ نہ پکڑا\n" +
"میں جب رستے سے بھٹکا تھا\n" +
"\n" +
"جو پایا ہے وہ تیرا ہے\n" +
"جو کھویا وہ بھی تیرا تھا\n" +
"\n" +
"تجھ بن ساری عمر گزاری\n" +
"لوگ کہیں گے تو میرا تھا\n" +
"\n" +
"پہلی بارش بھیجنے والے\n" +
"میں ترے درشن کا پیاسا تھا",
"تو جب میرے گھر آیا تھا\n" +
"میں اک سپنا دیکھ رہا تھا\n" +
"\n" +
"تیرے بالوں کی خوشبو سے\n" +
"سارا آنگن مہک رہا تھا\n" +
"\n" +
"چاند کی دھیمی دھیمی ضو میں\n" +
"سانوا مکھڑا لَو دیتا تھا\n" +
"\n" +
"تیری نیند بھی اُڑی اُڑی تھی\n" +
"میں بھی کچھ کچھ جاگ رہا تھا\n" +
"\n" +
"میرے ہاتھ بھی سلگ رہے تھے\n" +
"تیرا ماتھا بھی جلتا تھا\n" +
"\n" +
"دو روحوں کا پیاسا دل\n" +
"گرج گرج کر برس رہا تھا\n" +
"\n" +
"دو یادوں کا چڑھتا دریا\n" +
"ایک ہی ساگر میں گرتا تھا\n" +
"\n" +
"دل کی کہانی کہتے کہتے\n" +
"رات کا آنچل بھیگ چلا تھا\n" +
"\n" +
"رات گئے سویا تھا لیکن\n" +
"تجھ سے پہلے جاگ اُٹھا تھا"};
public void goToNextActivity(int positionOfGhazal){
GHAZAL_POSITION = positionOfGhazal;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ghazals);
tvGhazals = (TextView) findViewById(R.id.textView_ghazal);
tvGhazals.setText(ghazals[GHAZAL_POSITION]);
Typeface tf = Typeface.createFromAsset(this.getAssets(), "/fonts/NASTALIQ.TTF");
//tvGhazals.setTypeface(tf); */
}
}
And finally, here is the startar MainActivity:
package com.neatsoft.nasirkazmipehlibarish;
import android.content.Intent;
import android.graphics.Typeface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private static ListView listView;
private static String[] list = new String[] {
"میں نے جب لکھنا سیکھا تھا",
"تو جب میرے گھر آیا تھا",
"میں جب تیرے گھر پہنچا تھا",
"شام کا شیشہ کانپ رہا تھا",
"دن کا پھول ابھی جاگا تھا",
"پتھر کا شہر وہ بھی کیا تھا",
"پچھلے پہر کا سناٹا تھا",
"گرد نے خیمہ تھام لیا تھا",
"تُو جب دوبارہ آیا تھا",
"تجھ بن گھر کتنا سونا تھا",
"دھوپ تھی اور بادل چھایا تھا",
"دم ہونٹوں پر آکے رکا تھا",
"چاند ابھی تھک کر سویا تھا",
"نئے دیس کا رنگ نیا تھا",
"چھوٹی رات سفر لمبا تھا",
"تھوڑی دیر کو جی بہلا تھا",
"میں تیرے شہر سے پھر گزرا تھا",
"میں اس شہر میں کیوں آیا تھا",
"پل پل کانٹا سا چبھتا تھا",
"روتے روتے کون ہنسا تھا",
"پون ہری جنگل بھی ہرا تھا",
"تنہائی کا دکھ گہرا تھا",
"تیرا قصور نہیں میرا تھا"
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView();
}
public void ListView(){
listView = (ListView) findViewById(R.id.listView);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.titles_list, list);
listView.setAdapter(adapter);
listView.setOnItemClickListener(
new AdapterView.OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String value = (String) listView.getItemAtPosition(position);
Toast.makeText(MainActivity.this, position + "", Toast.LENGTH_LONG).show();
Ghazals ghazal = new Ghazals();
ghazal.goToNextActivity(position);
Intent intent = new Intent("com.neatsoft.nasirkazmipehlibarish.Ghazals");
startActivity(intent);
}
}
);
}
}
may be I am giving wrong path that it doesn't find the target font. I tried many combinations but they didn't work e.g:
Typeface tf = Typeface.createFromAsset(this.getAssets(), "/fonts/NASTALIQ.TTF");
Typeface tf = Typeface.createFromAsset(this.getAssets(), "fonts/NASTALIQ.TTF");
Typeface tf = Typeface.createFromAsset(this.getAssets(), "/NASTALIQ.TTF");
Typeface tf = Typeface.createFromAsset(this.getAssets(), "NASTALIQ.TTF");
Typeface tf = Typeface.createFromAsset(this.getAssets(), "assets/fonts/NASTALIQ.TTF");
Upvotes: 3
Views: 256
Reputation: 559
Move your font file from to main/assets/fonts/NASTALIQ.TTF
if any folder doesn't exist, create it.
Upvotes: 4