I have my own font that I want to use for my app in all layouts, I want to change the default font for my app.
In styles.xml I can use
<item name="android:fontFamily"></item>
to change the change the font, but how do I use my custom font here?
In App -> src -> main, i made a new directory called Assets, then another directory in that one called Fonts, and this is where i placed my custom font.
Is this possible? If so, how?
EDIT
As mayosk said, I added
compile 'uk.co.chrisjenx:calligraphy:2.2.0'
And made a Java Class that extends Application as shown below
public class CustomResources extends Application {
#Override
public void onCreate() {
super.onCreate();
CalligraphyConfig.initDefault(new CalligraphyConfig.Builder()
.setDefaultFontPath("fonts/din_light.ttf")
.setFontAttrId(R.attr.fontPath)
.build()
);
}
}
But the font is still the same, did I miss something
Use caligraphy :
https://github.com/chrisjenx/Calligraphy
Add dependency to your app build.gradle:
compile 'uk.co.chrisjenx:calligraphy:2.2.0'
and extend your application class where you need to add this code to onCreate method
CalligraphyConfig.initDefault(new CalligraphyConfig.Builder()
.setDefaultFontPath("Fonts/customFont.ttf")
.setFontAttrId(R.attr.fontPath)
.build()
);
and also you need to override attachBaseContext method() in your activity:
#Override
protected void attachBaseContext(Context newBase) {
super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase));
}
First of all you need to create a base class which extends Textview, here is the baseclass
public class MyTextView extends TextView {
public MyTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setTypeFace(context);
}
public MyTextView(Context context, AttributeSet attrs) {
super(context, attrs);
setTypeFace(context);
}
public MyTextView(Context context) {
super(context);
setTypeFace(context);
}
private void setTypeFace(Context context) {
setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/Lato-Bold.ttf"));
}}
later in xml
<com.example.MyTextView
android:id="#+id/text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="hi" />
same for edittext also. By doing this all the textviews and edittexts have same font entire the app.
I have a special way to change the default font.I set a whole paint to create custom view and draw font on onDraw method,instead of xml way。
like this:
`private void initPaint(Context context)
{
mTextPaint = new TextPaint();
mTextPaint.setTextSize(66.46F);
mTextPaint.setColor(Color.BLACK);
Typeface typeface = Typeface.createFromAsset(context.getAssets(), "fonts/MFKeKe_Noncommercial-Regular.ttf");
mTextPaint.setTypeface(typeface);
mTextPaint.setTextSkewX(-0.5F);
}
#Override
protected void onDraw(Canvas canvas)
{
super.onDraw(canvas);
mStaticLayout = new StaticLayout(TEXT1,mTextPaint,canvas.getWidth(), Layout.Alignment.ALIGN_NORMAL,1.0F,0.0F,false);
mStaticLayout.draw(canvas);
canvas.restore();
}`
But in usual,
For anyone still have question, the real, good answer is here:
https://stackoverflow.com/a/16883281/1154026
Related
I want to change my android app font to custom font and change font size. include all of activities and objects and alert dialog.
How to do this?
figured it out by my self. This is the code I used. I create custom TextView which has custom font as default font.
use Custom Textview to your Andrid Application
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(), "font/chiller.ttf");
setTypeface(tf ,1);
}
}
Custom Font
If you want to change the font in your app then you require
font file i.e roboto.ttf
custom textview where the font has been applied to the text.
I would recommend this library Fontify
Just make a class that extend Application and also mention that class in the manifest of the app using name attribute in application tag.In that class onCreate use the below code.
TypefaceUtil.overrideFont(getApplicationContext(), "SERIF", "Roboto-Thin.ttf");
i have Helvetica Neue.ttf in asset Folder , How to set the Helvetica Neue textStyle on My Entire Applcation.
There is currently no way to do this with the Views that come with the Android SDK. You can set your View to use any of the Roboto fonts as per this answer, but you cannot set a custom font.
The way I typically tackle this problem is to create my own TextView that uses my font, like so:
public class MyFontTextView extends TextView {
public static final String FONT_PATH = "fonts/MyFont.ttf";
public MyFontTextView(Context context) {
super(context);
initFont();
}
public MyFontTextView(Context context, AttributeSet attrs) {
super(context, attrs);
initFont();
}
public MyFontTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initFont();
}
/**
* Set up the font.
*/
private void initFont() {
if (!isInEditMode()) {
Typeface font = Typeface.createFromAsset(getContext().getAssets(), FONT_PATH);
setTypeface(font);
}
}
}
You replace all of your TextViews with this TextView, and then you will have your font. Note that other UI elements (e.g. Buttons) will still use Roboto unless you also customize those.
If you have a View that you only use once in your application, you could call setTypeFace() on that View instead of creating a custom View. The custom View method works well for Views that you use a lot in an application such as TextViews.
F1! i am working on another activity of android app and in this activity i applied custom font in 8 text views in urdu language, but it take 12 to 15 seconds for displaying this activity and if i remove custom font then it take 1-2 seconds for displaying this activity with by-default font but by-default android urdu language font is not pretty good. Remember that i never used any activity of font in .xml file. Any idea ?
My Java code:
public class JismPics extends Activity {
TextView txtView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.jism_pics);
txtView = (TextView) findViewById(R.id.textResult);
Typeface myCustomFont = Typeface.createFromAsset(getAssets(), "fonts/noori.ttf");
txtView.setTypeface(myCustomFont);
}
}
I use custom font in many of the apps I develope and never have that problem. Maybe you can override the views so you don't have to change the font on the onCreate() method
Something like this:
public class MyTextView extends TextView
{
public MyTextView(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
Typeface font = Typeface.createFromAsset(context.getAssets(), "fonts/NeoSans.otf");
this.setTypeface(font);
}
public MyTextView(Context context, AttributeSet attrs)
{
super(context, attrs);
Typeface font = Typeface.createFromAsset(context.getAssets(), "fonts/NeoSans.otf");
this.setTypeface(font);
}
public MyTextView(Context context)
{
super(context);
Typeface font = Typeface.createFromAsset(context.getAssets(), "fonts/NeoSans.otf");
this.setTypeface(font);
}
}
Don't forget to call your custom class on the layout.
<com.myproject.util.guicomponents.MyTextView
android:id="#+id/txt_name"
android:textSize="22sp"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="#string/txt_reg_name"
android:layout_gravity="center_vertical"
android:layout_marginLeft="15dp"
/>
Hi I am using a custom Typeface in my app with the following code:
public Typeface font;
//Activity on create:
font = Typeface.createFromAsset(getAssets(),"DINOT-Medium.otf");
TextView tv = (TextView)vi.findViewById(R.id.textView1);
tv.setTypeface(font);
The problem is after a while it gets buggy: the text can no longer be read and only squares are visible. Do you know why this happens? How can it be fixed?
Thanks
Create a Custom TextView like this :
public class DINOTMediumTextView extends TextView {
public DINOTMediumTextView(Context context) {
super(context);
setCustomFont(context);
}
public DINOTMediumTextView(Context context, AttributeSet attrs) {
super(context, attrs);
setCustomFont(context);
}
public DINOTMediumTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setCustomFont(context);
}
private void setCustomFont(Context context) {
Typeface tf = Typeface.createFromAsset(context.getAssets(), "fonts/DINOT-Medium.otf");
setTypeface(tf);
}
}
put the font's file in assets/fonts/ ( create a folder inside the assets folder)
and then in your layout xml :
<com.yourapp.views.DINOTMediumTextView
android:id="blabla"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
com.yourapp.views is the name of the package that contains your DINOTMediumTextView class.
On Mobiletuts+ there is very good tutorial on Text formatting for Android. Quick Tip: Customize Android Fonts
EDIT: Tested it myself now. Here is the solution. You can use a subfolder called fonts but it must go in the assets folder not the res folder. So
assets/fonts
Also make sure that the font ending I mean the ending of the font file itself is all lower case. In other words it should not be myFont.TTF but myFont.ttf
I have developed a very huge application and now i have a requirement of having custom font for all controls in the application. so I want to know the better way to change the font in one shot. The application has more than a hundred XML layout. and i cant change all controls to a custom component with custom font. Please provide a solution to Change the font without altering all the controls in XML.
Do something like this
pacage com.prac;
class MyFontedTextView extends TextView {
public FontedTextView(Context context) {
super(context);
init();
}
public FontedTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public FontedTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
private void init() {
String otfName = "MyCustomOtfFileWhichIPutInAssetsFolder.otf";
Typeface font = Typeface.createFromAsset(context.getAssets(), otfName);
this.setTypeface(font);
}
}
Now replace this all over in xml file from your TextViews
<com.prac.MyFontedTextView .... instead of <TextView
This change you have to do all over for it to apply
also for the case of button text . Button is also subclass of TextView
So the same can work for button's too
Hope this help or can lead you to the solution you are looking