A lot of extra padding to the left of toolbar title

Viewed 436

I have a toolbar that exists across fragments. The first fragment has no up navigation, but when I navigate to the second fragment I make the "up" arrow appear (to go back to the first fragment). However, when I go back to the first fragment and the up arrow disappears, there is a ton of padding to the left of the title. (The gif loops, watch for the 'Fragment 1' title to be spaced far from the left)

enter image description here

I think this has something to do with having android:animateLayoutChanges="true" set on my toolbar, because if I remove that line the padding disappears:

enter image description here

I can't figure out the cause of this, if anyone has a solution that may be able to help I would really appreciate it; whether that be finding a different way to animate the toolbar, or how to get rid of the padding.

Here are the things I've tried so far:

  • Getting the relative position of the title text before the fragment transition, then doing toolbarTitle.setLeft(oldLeft), but the oldLeft value is never set.
  • Trying to remove the padding from the toolbar by putting app:contentInsetLeft="0dp" app:contentInsetStart="0dp" in my toolbar definition, but this only removes 16dp of padding.

I don't think it matters, but here is my toolbar code. I populate the actions in each fragment's onCreateOptionsMenu.

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

<TextView
    style="@style/TextAppearance.AppCompat.Widget.ActionBar.Title"
    android:id="@+id/toolbar_title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="DEFAULT"
    android:gravity="start"
    android:textColor="@color/white" />

1 Answers

I know this is a hack, but I couldn't find anything online so this actually works for me:

 toolbar.post {
            val childCount = toolbar.childCount
            for (i in 0..childCount) {
                val child = toolbar.getChildAt(i)
                if (child is TextView) {
                    if (toolbar?.navigationIcon == null) {
                        val animator = ObjectAnimator.ofInt(child, "left", toolbar.contentInsetStart)
                        animator.duration = 300
                        animator.start()
                    }
                }
            }
        }

Basically, when there's no icon displayed in the toolbar (After navigating), I use toolbar.contentnInsetStart with ObjectAnimator to move back the title with an animation back to its original position.

One thing to note, is animator.duration should be the same duration of animateLayoutChanges, so if you're using a custom animator where the duration is different, use that instead, cause if you use anything below the title will snap back to the wrong position.

Related