117

I want to change the size of text in Toolbar. Because in my application, Toolbar text has different sizes both for landscape and portrait mode.

Is it possible to change the text size of text in Toolbar?

Vukašin Manojlović
  • 3,717
  • 3
  • 19
  • 31
Apurva
  • 7,871
  • 7
  • 40
  • 59

5 Answers5

341

Use app:titleTextAppearance:

<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="?actionBarSize"
    android:id="@+id/toolbar"
    app:titleTextAppearance="@style/Toolbar.TitleText" />

and override the default title size in a custom style:

<style name="Toolbar.TitleText" parent="TextAppearance.Widget.AppCompat.Toolbar.Title">
    <item name="android:textSize">18sp</item>
</style>

Result:

Screenshot

Vukašin Manojlović
  • 3,717
  • 3
  • 19
  • 31
29

for example this your toolbar

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    android:gravity="center"
    android:minHeight="?attr/actionBarSize"
    app:theme="@style/ThemeOverlay.AppCompat.ActionBar" />

you can simple add this view this

    app:titleTextAppearance="@style/yourstyle"

style.xml

  <style name="yourstyle" parent="@style/Base.TextAppearance.AppCompat.Title">
    <item name="android:textSize">20sp</item>
</style>

like that...

massaimara98
  • 7,401
  • 1
  • 18
  • 18
7

You can add a TextView to your toolbar and customize as you want:

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_height="wrap_content"
    android:layout_width="match_parent">
    <TextView
        android:id="@+id/toolbar_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/app_name"
        android:textSize="25sp"/>
</android.support.v7.widget.Toolbar>
Simas
  • 43,548
  • 10
  • 88
  • 116
  • 4
    This seems to be an answer that suits apps that support API < 21. the titleTextAppearance is a nice solution but only works API 21 and above. However, there are a few intricacies that we need to deal with going with this solution. If you are using SearchView, then the expanded version of the view will not stretch till the left end. Instead it will only stretch till the right edge of the newly added title text view. So you will have to do some customization there but again this is a specific case. In a general sense this is a good solution for any platform version. – Deepak G M Feb 12 '16 at 07:32
6

[Optional steps 1 & 2)

  1. Place your font file in "Project/app/src/main/assets/fonts/" folder.

Then create a Typeface object from the font file as:

Typeface typeFace = Typeface.createFromAsset(getAssets(), "fonts/font_name.ttf");
  1. Now, get the child item (toolbar title textview) from the existing toolbar, the default toolbar and pass the typeface object we created above.
((TextView) toolbar.getChildAt(0)).setTypeface(typeFace); // set custom typeface - font
  1. Finally, change the text size of toolbar's title text view
((TextView) toolbar.getChildAt(0)).setTextSize(30); // set title font size
OMi Shah
  • 5,768
  • 3
  • 25
  • 34
  • 2
    Welcome to SO! Please add some description to support your answer. – Mahib Feb 13 '19 at 21:08
  • 1
    this answer came in Low Quality posts in SO.... Could you add any commentary to your answer? Explain your logic, and give a little commentary on what your code is intended to do. This will help the OP, but it will also serve as commentary for future users – Ram Ghadiyaram Feb 14 '19 at 06:30
  • If you have navigation icon, you should use getChildAt(1) :) – Gabor Feb 24 '20 at 15:08
3

Try this in onCreate:

android.support.v7.app.ActionBar actionbar = getSupportActionBar();
TextView textview = new TextView(MainActivity.this);
RelativeLayout.LayoutParams layoutparams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);

textview.setLayoutParams(layoutparams);
textview.setText(getString(R.string.your_string));
textview.setTextColor(Color.WHITE);
textview.setTextSize(18);

actionbar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
actionbar.setCustomView(textview);

This is taken from Android-Examples.com.

Vukašin Manojlović
  • 3,717
  • 3
  • 19
  • 31
MorZa
  • 2,215
  • 18
  • 33