Custom font used in entire app - android

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.

Related

How to change app font to custom font

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

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"
/>

Android Custom Typeface

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

Android - How to set a custom font for whole app [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Is it possible to set font for entire Application?
I need to set a custom font (.ttf format) for whole app, how can i do it?
From Manifest or XML would be the best option if it is possible
EDIT Sept 2014:
For anyone still seeing this old terrible answer, the real, good answer is here:
https://stackoverflow.com/a/16883281/1154026
OLD:
Your best bet would be this:
Customize Android Fonts
So
TextView text = (TextView) findViewById(R.id.custom_font);
Typeface font = Typeface.createFromAsset(getAssets(), "yourfont.ttf");
text.setTypeface(font);
With your .ttf in the root of the "assets" folder.
You could do it like this.
Create a custom textview and use that one everywhere
public class MyTextView extends android.widget.TextView
{
public MyTextView(Context context)
{
this(context, null);
}
public MyTextView(Context context, AttributeSet attrs)
{
this(context, attrs, 0);
}
public MyTextView(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs);
if (this.isInEditMode()) return ;
final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.SomeStyle);
final String customFont = a.getString(R.styleable.SomeStyle_font);
//Build a custom typeface-cache here!
this.setTypeface(
Typeface.createFromAsset(context.getAssets(), customFont)
);
}
}
Add this to attrs.xml
<declare-styleable name="SomeStyle">
<attr name="font" format="string" />
</declare-styleable>
Then in your theme, do this:
This will make sure all the textviews will use the style MyTextView
<item name="android:textViewStyle">#style/MyTextView</item>
And now we can define your custom font using your custom attribute in this style.
<style name="MyTextView" parent="#android:style/Widget.TextView">
<item name="font">MyPathToFontInAssets.ttf</item>
</style>
So whenever you use MyTextView in the project, it will have your custom font.
IMPORTANT: Typefaces are not cached, so if you plan on using this code, you should also build a custom typeface-cache so you can re-use the custom typeface for all textviews. This will significantly speed-up the application!
UPDATE: As Amir was saying, this is almost the same as Custom fonts and XML layouts (Android) but i also use android styling to automatically use it on all textviews in the app.
Cerate a TextView subclass and use it everywhere
public class RobotoTextView extends TextView {
public RobotoTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
public RobotoTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public RobotoTextView(Context context) {
super(context);
init();
}
private void init() {
if (!isInEditMode()) {
Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "fonts/Roboto-Light.ttf");
setTypeface(tf);
}
}
}
usage
<com.test.RobotoTextView
android:id="#+id/name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
in java code you can cast it to TextView
TextView tv=(TextView)findViewById(R.id.custom);
Typeface face=Typeface.createFromAsset(getAssets(),
"fonts/Verdana.ttf");
tv.setTypeface(face);
put the font file in the fonts folder in /res/
Like my answer if it is helpful...
You can extend the TextView as suggested here and set your typeface on that then use it in whole app.

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