Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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>
like image 970
Ololoking Avatar asked Feb 17 '16 08:02

Ololoking


People also ask

How do I change the title size on my toolbar?

Typeface typeFace = Typeface. createFromAsset(getAssets(), "fonts/font_name. ttf"); Now, get the child item (toolbar title textview) from the existing toolbar, the default toolbar and pass the typeface object we created above.


3 Answers

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" />
like image 157
CodeChimp Avatar answered Oct 19 '22 02:10

CodeChimp


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.

like image 1
alidrsn Avatar answered Oct 19 '22 02:10

alidrsn


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" />
like image 1
sunadorer Avatar answered Oct 19 '22 02:10

sunadorer