Add the .ttf file in your assets/fonts folder. To use it in TextView the best way is to extend the TextView and have to set the font. Here is the code of the extended TextView,
package naveendroid.fontstest;
import android.content.Context;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.widget.TextView;
public class RobotTextView extends TextView {
public RobotTextView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle);
init();
}
public RobotTextView(Context context, AttributeSet attrs) { super(context, attrs);
init();
}
public RobotTextView(Context context) { super(context);
init();
}
private void init() {
Typeface tf = Typeface.createFromAsset(getContext().getAssets(),
"Roboto-Regular.ttf");
setTypeface(tf);
}
}
Then use this in your .xml file as shown below,
<naveendroid.fontstest.MyTextView
android:id="@+id/sometext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_weight="1"
android:text="TextView with the font that you had set in RobotTextView class "
android:textSize="20dip"
android:textColor="#000"
>
No comments:
Post a Comment