Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing a font under assets folder from XML file in Android

I am trying to do a application-wide font change and creating a style file to do so. In this file (below) I just want to change typeface value of TextAppearance style of Android.

<?xml version="1.0" encoding="utf-8"?> <resources>     <style name="NightRiderFont" parent="@android:style/TextAppearance">         <item name="android:typeface"> /***help need here***/ </item>     </style> </resources> 

However font is in "assets/fonts/". How can I access this font, so I can use that style as a theme to get rid of changing all TextViews by hand programatically.

As summary: How can I access 'a file from assets folder' in XML?

like image 793
erkangur Avatar asked Jul 19 '11 11:07

erkangur


People also ask

How do you use fonts in assets?

Step 1: Create a new asset folder(app/New/Folder/Asset folder) in Android Studio and paste the 'ttf' file of the font here. The picture on the left shows how to add the assets folder to the project whereas the picture on the right shows the added 'ttf' file to it.

Where is font folder in Android?

1 - Right-click the res folder and go to New > Android resource directory. The New Resource Directory window appears. 2 - In the Resource type list, select font, and then click OK. 3 - Add your font files in the font folder just by a simple copy and paste.

How to add custom fonts to XML file in Android?

To use fonts support in XML feature on devices running Android 4.1 (API level 16) and higher, use the Support Library 26+. Right click res folder, new -> Android resource directory-> select font -> Ok. put your "myfont.ttf" file in newly created font folder.

How to read a file from assets in Android?

This example demonstrates how do I read a file from assets in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main.xml. Step 3 – Right click app >> New >> Folder >> Assets folder.

How to create an asset folder in Android Studio?

Step 1: To create an asset folder in Android studio open your project in Android mode first as shown in the below image. Step 2: Go to the app > right-click > New > Folder > Asset Folder and create the asset folder. Step 3: Android Studio will open a dialog box.

How do I access the font file?

You can access your font file from assets folder to xml file. fonts is the sub folder in assets folder. Thanks for contributing an answer to Stack Overflow! Please be sure to answer the question.


1 Answers

In my research, there is no way to add external font to the xml file. Only the 3 default font is available in xml

But you can use in java using this code.

Typeface tf = Typeface.createFromAsset(getAssets(),"fonts/verdana.ttf");   textfield.setTypeface(tf,Typeface.BOLD); 

Update:

Now I find a way to do this by creating a custom class extending the TextView and use that in the xml file.

public class TextViewWithFont extends TextView {     private int defaultDimension = 0;     private int TYPE_BOLD = 1;     private int TYPE_ITALIC = 2;     private int FONT_ARIAL = 1;     private int FONT_OPEN_SANS = 2;     private int fontType;     private int fontName;      public TextViewWithFont(Context context) {         super(context);         init(null, 0);     }     public TextViewWithFont(Context context, AttributeSet attrs) {         super(context, attrs);         init(attrs, 0);     }     public TextViewWithFont(Context context, AttributeSet attrs, int defStyle) {         super(context, attrs, defStyle);         init(attrs, defStyle);     }     private void init(AttributeSet attrs, int defStyle) {         // Load attributes         final TypedArray a = getContext().obtainStyledAttributes(                 attrs, R.styleable.font, defStyle, 0);         fontName = a.getInt(R.styleable.font_name, defaultDimension);         fontType = a.getInt(R.styleable.font_type, defaultDimension);         a.recycle();         MyApplication application = (MyApplication ) getContext().getApplicationContext();         if (fontName == FONT_ARIAL) {             setFontType(application .getArialFont());         } else if (fontName == FONT_OPEN_SANS) {             setFontType(application .getOpenSans());         }     }     private void setFontType(Typeface font) {         if (fontType == TYPE_BOLD) {             setTypeface(font, Typeface.BOLD);         } else if (fontType == TYPE_ITALIC) {             setTypeface(font, Typeface.ITALIC);         } else {             setTypeface(font);         }     } } 

and in xml

<com.example.customwidgets.TextViewWithFont         font:name="Arial"         font:type="bold"         android:layout_width="wrap_content"         android:text="Hello world "         android:padding="5dp"         android:layout_height="wrap_content"/> 

dont forget to add the schema in root of your xml

xmlns:font="http://schemas.android.com/apk/res-auto" 

And create an attrs.xml file inside values directory, which is holding our custom attribues:

<?xml version="1.0" encoding="utf-8"?> <resources>     <declare-styleable name="font">         <attr name="type">         <enum name="bold" value="1"/>             <enum name="italic" value="2"/>         </attr>         <attr name="name">             <enum name="Arial" value="1"/>             <enum name="OpenSans" value="2"/>         </attr>     </declare-styleable> </resources> 

Update:

Found some performance issue when this custom view is used in listview, that is because the font Object is creating every time the view is loaded. Solution I found is to initialize the font in Application Class and refer that font object by

MyApplication application = (MyApplication) getContext().getApplicationContext(); 

Application class will look like this

public class MyApplication extends Application {      private Typeface arialFont, openSans;      public void onCreate() {         super.onCreate();          arialFont = Typeface.createFromAsset(getAssets(), QRUtils.FONT_ARIAL);         openSans = Typeface.createFromAsset(getAssets(), QRUtils.FONT_OPEN_SANS);     }      public Typeface getArialFont() {         return arialFont;     }      public Typeface getOpenSans() {         return openSans;     } } 
like image 150
droid kid Avatar answered Oct 07 '22 01:10

droid kid