Reputation: 145
I am using android custom font lib Calligraphy https://github.com/chrisjenx/Calligraphy.
But there is no effect on textview. I am using the below code:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
protected void attachBaseContext(Context newBase) {
super.attachBaseContext(new CalligraphyContextWrapper(newBase));
}
}
In XML:
<TextView
tools:context=""
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Touch Listner"
android:textSize="18sp"
app:fontPath="fonts/gotham-book.ttf" />
In attrs:
<resources>
<attr name="fontPath" format="string"/>
</resources>
In assets/font/gotham-book.ttf
Upvotes: 5
Views: 9268
Reputation: 1009
I had the same problem.
Here, You need to do two things.
Firstly,in xml, add two lines (as shown in below) in root of layout
xmlns:tools="http://schemas.android.com/tools"
tools:ignore="MissingPrefix"
And last, in Activity class,
protected void attachBaseContext(Context newBase) {
super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase));
}
Yooooo, it's done !! Simple
Example please check https://i.sstatic.net/F87JE.png
Upvotes: 1
Reputation: 1435
Create the class that extends Application
.
Something like that:
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
CalligraphyConfig.initDefault(new CalligraphyConfig.Builder()
.setDefaultFontPath("fonts/DINNextLTPro-Regular.otf")
.setFontAttrId(R.attr.fontPath)
.build()
);
}
}
The next thing we need to do is to open the AndroidManifest.xml
file of our application and add a reference to MyApplication
in the android:name
attribute of the application tag, so in the end the manifest file will look similar to this one:
<?xml version="1.0" encoding="utf-8"?>
<manifest package="it.sbsmobile.golife"
xmlns:android="http://schemas.android.com/apk/res/android">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme"
android:name="com.example.MyApplication">
<activity
android:name=".Profile1"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>
Also, the instructions at https://github.com/chrisjenx/Calligraphy say to inject into the context, by overriding a method in the activity as follows:
@Override
protected void attachBaseContext(Context newBase) {
super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase));
}
Source: LINK
Why extend Application class? ANSWER
Upvotes: 3
Reputation: 3058
You can also use simple EasyFonts third party library to set variety of custom font to your TextView
. By using this library you should not have to worry about downloading and adding fonts into the assets/fonts folder. Also about Typeface object creation.
Provided font faces. Which might you would like to use.
Simply:
TextView myTextView = (TextView)findViewById(R.id.myTextView);
myTextView.setTypeface(EasyFonts.robotoThin(this));
Upvotes: 0
Reputation: 13139
Remove the namespace from the TextView
.
<TextView
tools:context=""
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Touch Listner"
android:textSize="18sp"
fontPath="fonts/gotham-book.ttf" />
(Change app:fontPath
to fontPath
)
Annoyingly we can't resolve the auto-res namespace.
Upvotes: 5
Reputation: 145
Create the class that extends Application, now and the following code in it. and declare it in manifest under application tag.
CalligraphyConfig.initDefault(new CalligraphyConfig.Builder()
.setDefaultFontPath("fonts/Roboto-ThinItalic.ttf")
.setFontAttrId(R.attr.fontPath)
.build()
);
Upvotes: 4
Reputation: 107
1) Download and copy your font (yourfont.ttf) file in the assets folder of your project
2) In your Activity/Fragment use the following code to load the font
Typeface yourfont = Typeface.createFromAsset(getAssets(),
"fonts/yourfont.ttf");
3) Find your TextView(or any other view on which you want to set this font) and use the setTypeface method of the View. eg.
TextView textview1 = (TextView) findViewById(R.id.exampletextview);
textview1.setTypeface(yourfont);
This of course comes with a drawback, that if there are a lot of views you want to apply the font to you would have manually setTypeface for each of the view. In that case you can make a custom view by extending the Textview class. Something like the code below :
public class MyTextView extends TextView {
public MyTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
public MyTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public MyTextView(Context context) {
super(context);
init();
}
public void init() {
Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "fonts/yourfont.ttf");
setTypeface(tf ,1);
}
}
and use this in xml as <com.yourpackagename.MyTextView />
Upvotes: 2
Reputation: 2309
I've used TypefaceTextView lib to obtain the results you want. Please, take a look at (https://github.com/ragunathjawahar/android-typeface-textview).
Upvotes: 0