Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6151925
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T19:50:27+00:00 2026-05-23T19:50:27+00:00

I am trying to do a application-wide font change and creating a style file

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T19:50:28+00:00Added an answer on May 23, 2026 at 7:50 pm

    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;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to use an NSFontPanel to allow the user to change an application-wide
I hit this error while my web application was trying to execute a SELECT
I'm writing an ASP.NET MVC application and trying to use a RESX file to
I am trying to create an application that will send out style-heavy emails and
I am creating database Entities in my Java application and trying to rationalize between
I have a java application trying to post to a php file on an
Well I've seen a wide variety of failures while trying to get this to
I am trying bind my application just to my VPS. Can you please tell
I'm building a somewhat large application using kohana 3.1. I'm trying to set module-wide
I guess this question is somewhat wide but I'm trying to create a C

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.