How to set a custom font to the title in toolbar android

anupam x

I am doing this:

toolbar = (Toolbar) findViewById(com.sports.unity.R.id.tool_bar);
setSupportActionBar(toolbar);
setTitle("hello");

I want to set a custom font for the text here in the title "hello". How to do that?

gmetax

Update 2018 (kotlin version)

fun Toolbar.changeToolbarFont(){
    for (i in 0 until childCount) {
        val view = getChildAt(i)
        if (view is TextView && view.text == title) {
            view.typeface = Typeface.createFromAsset(view.context.assets, "fonts/customFont")
            break
        }
    }
}

and use it like that toolBar.changeToolbarFont()

old-post

To use a custom title in your Toolbar all you need to do is remember is that Toolbar is just a fancy ViewGroup so you can add a custom title like so:

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar_top"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:minHeight="?attr/actionBarSize"
    android:background="@color/action_bar_bkgnd"
    app:theme="@style/ToolBarTheme" >


     <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Toolbar Title"
        android:layout_gravity="center"
        android:id="@+id/toolbar_title" />


    </android.support.v7.widget.Toolbar>

This means that you can style the TextView however you would like because it's just a regular TextView. So in your activity you can access the title like so:

Toolbar toolbarTop = (Toolbar) findViewById(R.id.toolbar_top);
TextView mTitle = (TextView) toolbarTop.findViewById(R.id.toolbar_title);

And then:

Typeface khandBold = Typeface.createFromAsset(BalrogApplication.getApplication().getAssets(), "fonts/Khand-bold.ttf");

mTitle.setTypeface(khandBold);

UPDATE dynamically version

public static void changeToolbarFont(Toolbar toolbar, Activity context) {
    for (int i = 0; i < toolbar.getChildCount(); i++) {
        View view = toolbar.getChildAt(i);
        if (view instanceof TextView) {
            TextView tv = (TextView) view;
            if (tv.getText().equals(toolbar.getTitle())) {
                applyFont(tv, context);
                break;
            }
        }
    }
}

public static void applyFont(TextView tv, Activity context) {
    tv.setTypeface(Typeface.createFromAsset(context.getAssets(), "fonts/customFont"));
}

and use it like that

changeToolbarFont(findViewById(R.id.app_bar), this);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Android toolbar center title and custom font

From Dev

How to set a custom font from the assets folder to the title of an Actionbar in Android?

From Dev

Set Android Custom Font on Tab Title

From Dev

Custom font on toolbar in android

From Dev

Android Toolbar set title explanation

From Dev

Change font size of Toolbar title in CollapsingToolbarLayout in android

From Dev

Change font size of Toolbar title in CollapsingToolbarLayout in android

From Dev

How to change android title color with Coordinator layout and custom toolbar

From Dev

How can i set Toolbar title using Retrofit android?

From Dev

How to set custom font and weight (style) to UInavigationbar title

From Java

How to set custom font for a whole application in Android?

From Dev

How android set custom font in canvas?

From Dev

How android set custom font in canvas?

From Dev

Android: How to give custom font style to action bar title

From Dev

Android: when to set Toolbar title form Fragment?

From Dev

Set a title in Toolbar from fragment in Android

From Dev

Android: when to set Toolbar title form Fragment?

From Dev

Centering Title in Custom Toolbar

From Dev

Android - How to center a title (TextView) in a toolbar?

From Dev

Set toolbar title

From Dev

How to check if an android device has a custom font set

From Java

How to set menu to Toolbar in Android

From Dev

How to set Android toolbar height?

From Dev

How to set the font size of navigation bar title

From Dev

amCharts: How to set font family in chart title?

From Dev

How to set matplotlib font for title, axes,

From Dev

Android custom toolbar menu item title color and style

From Dev

Android custom toolbar menu item title color and style

From Dev

How to set custom font for backBarButtonItem

Related Related

  1. 1

    Android toolbar center title and custom font

  2. 2

    How to set a custom font from the assets folder to the title of an Actionbar in Android?

  3. 3

    Set Android Custom Font on Tab Title

  4. 4

    Custom font on toolbar in android

  5. 5

    Android Toolbar set title explanation

  6. 6

    Change font size of Toolbar title in CollapsingToolbarLayout in android

  7. 7

    Change font size of Toolbar title in CollapsingToolbarLayout in android

  8. 8

    How to change android title color with Coordinator layout and custom toolbar

  9. 9

    How can i set Toolbar title using Retrofit android?

  10. 10

    How to set custom font and weight (style) to UInavigationbar title

  11. 11

    How to set custom font for a whole application in Android?

  12. 12

    How android set custom font in canvas?

  13. 13

    How android set custom font in canvas?

  14. 14

    Android: How to give custom font style to action bar title

  15. 15

    Android: when to set Toolbar title form Fragment?

  16. 16

    Set a title in Toolbar from fragment in Android

  17. 17

    Android: when to set Toolbar title form Fragment?

  18. 18

    Centering Title in Custom Toolbar

  19. 19

    Android - How to center a title (TextView) in a toolbar?

  20. 20

    Set toolbar title

  21. 21

    How to check if an android device has a custom font set

  22. 22

    How to set menu to Toolbar in Android

  23. 23

    How to set Android toolbar height?

  24. 24

    How to set the font size of navigation bar title

  25. 25

    amCharts: How to set font family in chart title?

  26. 26

    How to set matplotlib font for title, axes,

  27. 27

    Android custom toolbar menu item title color and style

  28. 28

    Android custom toolbar menu item title color and style

  29. 29

    How to set custom font for backBarButtonItem

HotTag

Archive