'Toolbar subtitle is cut (It does not fit in height)

My question is very simple, but i can't figure it out:

When i try to display a subtitle, the toolbar has not enough space do display it and it is cut.

Toolbar:

<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="?android:actionBarSize"
android:layout_centerHorizontal="true"
app:theme="@style/MyToolBar">

Does ?android:actionBarSize have not enough height to display title and subtitle correctly?

When I set ToolBar height to wrap_content everything works, but can anyone explain to me how I should correctly set the height?

styles.xml :

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">

    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="android:windowBackground">@color/background1</item>
    <item name="android:textColorPrimary">@color/colorAccent</item>

    <!-- Customize your theme here. -->
</style>

<style name="MyToolBar" parent="Widget.AppCompat.ActionBar">
    <!-- Support library compatibility -->
    <item name="android:textColorPrimary">@color/colorAccent</item>
    <item name="android:textColorSecondary">@color/colorAccent</item>
    <item name="colorControlNormal">@color/colorAccent</item>

</style>


Solution 1:[1]

I have used a toolbar with subtitles and as well as using a layout_height="wrap_content" I also add a minHeight. This will ensure your toolbar is at least the minimum height. I've not seen it jump in height or crop but I always have a subtitle set.

<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    android:elevation="@dimen/default_elevation"
    app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

Solution 2:[2]

i suggest you to create a new layout for toolbar and then you can fill it however you want to.And set it as ActionBar in java code. Therefore, you will not face any space problem anymore.

Solution 3:[3]

I had used layout_height=actionBarSize for a long time even with subtitles present and never came across this issue (testing on various emulators and pixel phone). I applied this fix now after seeing my subtitle being cut-off on a Samsung Galaxy phone. Seems to me that this issue depends on how the system sets up the subtitle spacing.

I already had switched to the new material components (androidx). So the issue is still present in latest version.

Using the solution of a combination of wrap_content as height and the actionBarSize attribute as minHeight doesn't affect devices without the issue, so on those, no jumping will be visible.

<androidx.appcompat.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="?android:actionBarSize" />

Solution 4:[4]

The problem is that you have specified the layout height as ?attr/actionBarSize. This should be fine if you were not specifying a subtitle as this is tailor made for title only. My suggestion is to make android:layout_height="wrap_content" and instead use the minHeight parameter of the toolbar. Hence your xml becomes-

<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="wrap_content"
android:minHeight="?attr/actionBarSize"
android:layout_centerHorizontal="true"
app:theme="@style/MyToolBar">

Solution 5:[5]

In my case, I use a custom font (NotosanJP) for my project so my Toolbar sub title is cut of. Without using a custom font, it works well.
So my solution is to change the text size inToolbar.
After debug, I found that the default size of my current Toolbar title is 20 and Toolbar subtitle is 16. So I reduce it by 2

<androidx.appcompat.widget.Toolbar
            ...
            app:titleTextAppearance="@style/MyToolbarTitleAppearance"
            app:subtitleTextAppearance="@style/MyToolbarSubtitleAppearance" />

style.xml

<style name="MyToolbarTitleAppearance" parent="@style/TextAppearance.Widget.AppCompat.Toolbar.Title">
    <item name="android:textSize">18dp</item>
</style>

<style name="MyToolbarSubtitleAppearance" parent="@style/TextAppearance.Widget.AppCompat.Toolbar.Subtitle">
    <item name="android:textSize">14dp</item>
</style>

Solution 6:[6]

In addition to what the accepted answer suggests, adding android:minHeight="?attr/actionBarSize", I'd also suggest you add a bottom padding. In my case, if I didn't do it, depending on the system font size, the subtitle was almost cut by the end of the toolbar. If you have the space, just add android:paddingBottom="6dp"

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 CodeChimp
Solution 2 alidrsn
Solution 3
Solution 4 Vaibhav Sharma
Solution 5 Linh
Solution 6 Roc Boronat