How to change app font to custom font - android

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");

Related

Android - How to set familiy font "noto sans" in xml?

I'm developing an android widget app targeting for above Lollipop (Android 5.0) users.
I have to use a font which is called "noto sans"(https://www.google.com/get/noto/#/) in my app for some reasons.
I heard it is contained in Lollipop, but i don't know how to use it in xml.
Anyone knows about it?
No, you cannot use custom fonts in layout file (XML). You have to use the custom fonts programmatically as shown below
String fontPath = "fonts/calibrib.ttf";
// Loading Font Face
Typeface tf = Typeface.createFromAsset(mContext.getAssets(), fontPath);
TextView txtView = new TextView(this);
txtView.setTypeface(tf);
Place your font inside assets folder. Here is for what above code is working.
You could create your own TextView by overriding the TextView like this, you have to put the font in the assets folder:
public class MyTextView extends TextView {
public MyTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setType(context);
}
public MyTextView(Context context, AttributeSet attrs) {
super(context, attrs);
setType(context);
}
public MyTextView(Context context) {
super(context);
setType(context);
}
private void setType(Context context){
this.setTypeface(Typeface.createFromAsset(context.getAssets(),
"foo.ttf"));
this.setShadowLayer(1.5f, 5, 5, getContext().getResources().getColor(R.color.black_shadow));
}
}
And use it like this:
<com.your.project.package.MyTextView
android:id="#+id/oppinfo_mtv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:text="Player 1"
/>

how to use a same custom fontface for all the view in android?

i want use all the component in android which having the same font type face, for that i am creating a individual custom class for each component like CustomTextView, CustomEditText, etc,..
So instead of creating a individual class for each component can i create a view CustomView class that will automatically apply style for all the components in android
Just declare your own TextView and use it in your XML, it should appear in the custom Views
public class MyTextView extends TextView {
public MyTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setType(context);
}
public MyTextView(Context context, AttributeSet attrs) {
super(context, attrs);
setType(context);
}
public MyTextView(Context context) {
super(context);
setType(context);
}
private void setType(Context context){
this.setTypeface(Typeface.createFromAsset(context.getAssets(), "chalk_board.ttf"));
}
Oh dam u want it globally for all views, so this is the wrong approach.... Sorry for that
You have at least 2 ways:
create your own TextView class and set fontFace in constructor
you can use custom LayoutInflater. And every time view gets inflated check that it is textView (or other view not extending textView but having font settings) and set correct fontFace settings.

Custom font used in entire app

So there are different ways to set your font in Android.
What i need to do is to set the font in combination with a custom font and thereby over the entire app.
Now after some research i have 2 options.
Override EditText and set font, use this class in my layout xml.
Add a font to my AppTheme in my styles.xml.
Now i prefer the last option, this is obviously in my position the best way to go since i use the same font in my entire app.
Now the problem is, in my Assets/font/ folder i added the font i want to use but i can't use this in my styles.
<style name="AppTheme" parent="AppBaseTheme">
<item name="android:typeface"> Here? </item>
</style>
How to add my font as typeface. Is that possible?
Try this code:
public class CustomTextView extends EditText {
public CustomTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
if (!isInEditMode())
init();
}
public CustomTextView(Context context, AttributeSet attrs) {
super(context, attrs);
if (!isInEditMode())
init();
}
public CustomTextView(Context context) {
super(context);
if (!isInEditMode())
init();
}
public void init() {
Typeface tf = Typeface.createFromAsset(getContext().getAssets(), Constants.FONTPATH);
setTypeface(tf);
}
}
Then Use in XMLwith ;
<pkgname.CustomTextView android:id="#+id/txt" android:layout_width="wrap_content" android:layout_height="wrap_content" />
As far as I know there is no way we can access external assets in xml files.

Change fonts in assets directory in android Widget

Due to widget development there is a requrement in definition of my own font (wich can be changed sometimes via SharedProperties).
One fo the soultion i saw here is to use:
TextView tv=(TextView)findViewById(R.id.yearthree_view);
Typeface font = Typeface.createFromAsset(this.getAssets(), "fonts/Century_Gothic.ttf");
tv.setTypeface(font);
But the problem is: onUpdate() method dosn't familiar with
findViewById
My approach using RemoteViews to get to TextView in order to update text.
Thanks in advance.
Create a custom TextView extends TextView and set the Typeface when initialize.
public class CustomTextView extends TextView {
public CustomTextView(Context context, AttributeSet attrs) {
super(context, attrs);
initTextFont(context);
}
public CustomTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initTextFont(context);
}
private void initTextFont(Context context) {
//Set font here.
}
}
use context.getassets() ;
context is passed in onUpdate
Typeface font = Typeface.createFromAsset(context.getAssets(), "fonts/Century_Gothic.ttf");

android: how to set custom font for entire application

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

Categories

Resources