R
R
retr02017-05-23 18:40:42
Java
retr0, 2017-05-23 18:40:42

How to change default theme font to custom font in Android Studio?

In the most default application theme, which is automatically applied in Android Studio for each new project, a blue panel with the name of the application is displayed on top, under the status bar. How can I edit the font on this panel?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey Semenko, 2017-05-23
@retr0

Reflection to help:
https://stackoverflow.com/questions/2711858/is-it-...
The Typeface class has standard fonts defined, it is enough to override the desired values ​​and you will be happy.

public class Typeface {

    ...

    /** The default NORMAL typeface object */
    public static final Typeface DEFAULT;
    /**
     * The default BOLD typeface object. Note: this may be not actually be
     * bold, depending on what fonts are installed. Call getStyle() to know
     * for sure.
     */
    public static final Typeface DEFAULT_BOLD;
    /** The NORMAL style of the default sans serif typeface. */
    public static final Typeface SANS_SERIF;
    /** The NORMAL style of the default serif typeface. */
    public static final Typeface SERIF;
    /** The NORMAL style of the default monospace typeface. */
    public static final Typeface MONOSPACE;
    
    ...

}

You can also do something like this:
<android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay">
        
        <TextView
            android:id="@+id/toolbar_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
        
    </android.support.v7.widget.Toolbar>

Then set the font in the code:
Typeface typeface = Typeface.createFromAsset(context.getAssets(), fontAssetName);
TextView toolbarTitle = (TextView) findViewById(R.id.toolbar_title);
toolbarTitle.setTypeface(typeface);

D
Denis Zagaevsky, 2017-05-23
@zagayevskiy

Android O will have the ability to use custom fonts.
The 26.0.0 Beta 1 Support Library release backports this behavior.
So you can play with it and do it normally, without reflection.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question