Reputation:
I am using a simple TextView
in my activity and want to make it appear in a custom font 'Black Chancery'. I have put the blkchcry.ttf
file at location app>src>main>assets>fonts>blkchcry.ttf. But, the android studio can't find that font. Here's my AboutusActivity.java
code
public class AboutusActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_aboutus);
TextView tx = (TextView)findViewById(R.id.title);
Typeface custom_font = Typeface.createFromAsset(getAssets(), "fonts/blkchcry.ttf");
tx.setTypeface(custom_font);
}
}
and following is the activity_aboutus.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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"
tools:context=".AboutusActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="About Rathi Classes"
android:textSize="20sp"
android:layout_margin="10dp"
android:gravity="center_horizontal"/>
<TextView
android:layout_below="@+id/title"
android:id="@+id/description"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/about_text"
android:textSize="20sp"
android:layout_margin="10dp" />
</RelativeLayout>
and here is the logcat
Process: net.softglobe.rathiclasses, PID: 8894
java.lang.RuntimeException: Unable to start activity ComponentInfo{net.softglobe.rathiclasses/net.softglobe.rathiclasses.AboutusActivity}: java.lang.RuntimeException: Font asset not found fonts/blkchcry.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/blkchcry.ttf
at android.graphics.Typeface.createFromAsset(Typeface.java:206)
at net.softglobe.rathiclasses.AboutusActivity.onCreate(AboutusActivity.java:17)
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)
I have tried cleaning the project but it failed to resolve the problem. Please help.
Upvotes: 1
Views: 6319
Reputation: 1136
In my case, I was adding a font with Capital Letters and some symbols like -(hyphen). Try renaming the fonts. So, just make all letters small, replace hyphen with underscore _
Upvotes: 0
Reputation: 2161
Paste Font here like this and also make your font name in small case :-
app > src > main > assets > fonts > blkchry.tff
set Font in java File like this :-
public class AboutusActivity extends Activity {
Context mContext;
Typeface fontblkchry;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mContext = this;
setContentView(R.layout.activity_about_us);
fontblkchry = Typeface.createFromAsset(mContext.getAssets(), "fonts/blkchry.ttf");
textview1.setTypeface(fontblkchry);
}
}
Upvotes: 1
Reputation: 3933
Change the location your blkchcry.tttf
from
app>src>main>assets>fonts>blkchcry.ttf.
to
app>src>main>res>font>blkchry.tff
And here's the fun part,
you need to create a custom font file inside the same folder.
Name it as my_font.xml
That's called Creating a Font Family
If you’re using multiple versions of the same font, then you may want to group them together into a font family. A font family is essentially a dedicated XML file where you define each version of the font, along with its associated style and weight attributes.
Here's a demo font file
[Update] which you can simply paste coz I updated the contents.
<?xml version="1.0" encoding="utf-8"?>
<font-family 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">
<font
android:font="@font/blkchry"
android:fontStyle="normal"
android:fontWeight="400"
app:font="@font/blkchry"
app:fontStyle="normal"
app:fontWeight="400"
tools:ignore="UnusedAttribute" />
</font-family>
Because getAssets()
will look for an .xml
file, not .tff
even though you mentioned .tff
file in your code.
Upvotes: 3
Reputation: 691
Change your location:
from
app>src>main>assets>fonts>blkchcry.ttf.
to
app>src>main>res>font>blkchry.tff
then change your typeface:
Typeface typeface = getResources().getFont(R.font.blkchcry); textView.setTypeface(typeface);
See more details: https://developer.android.com/guide/topics/ui/look-and-feel/fonts-in-xml
Upvotes: 0