0

I am using an activity which hosts a ListFragment. From the ListFragment I can open a DetailTabsFragment. The two tabs host two fragments: DetailFragment and ReviewFragment.

I set the title in DetailFragment, with this code:

((AppCompatActivity)getActivity()).getSupportActionBar().setTitle("my title");

But as you can see here in the screenshot, the title is truncated at the first character. Why is that?

ActionBar screenshot

The xml layout of DetailTabsFragment:

    <android.support.design.widget.AppBarLayout xmlns:android="http://schemas.android.com/apk/res/android"
                                            xmlns:tools="http://schemas.android.com/tools"
                                            xmlns:app="http://schemas.android.com/apk/res-auto"
                                            android:layout_width="match_parent"
                                            android:layout_height="match_parent"
    app:elevation="0dp">

    <android.support.design.widget.TabLayout
        android:id="@+id/tabLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimary"
        app:tabMode="fixed"
        app:tabGravity="fill"
        app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/white"/>

</android.support.design.widget.AppBarLayout>

An the mainActivity's layout:

<android.support.design.widget.CoordinatorLayout
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.dcs.shows.MainActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay"
        app:elevation="0dp">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay">

        </android.support.v7.widget.Toolbar>
    </android.support.design.widget.AppBarLayout>
Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
user11230
  • 539
  • 1
  • 9
  • 19

1 Answers1

1

I was having the same issue. My architecture is a bit different, but the end result of the title being truncated to a single letter (T...) was the same. I too played around with every suggestion on that other SO question. Ultimately, it seemed like some sort of strange race condition with the setting of the title in the activity. I found that simply overriding the activity's setTitle function and wrapping the call to super in a postDelay runnable fixed it for me:

@Override
public void setTitle(final CharSequence title) {
    toolBar.postDelayed(new Runnable() {
        @Override
        public void run() {
            MainActivity.super.setTitle(title);
        }
    }, 100);
}

I'm using toolbar's postDelayed as a convenience method. But you can certainly use a handler here. Hope this helps.

SBerg413
  • 14,515
  • 6
  • 62
  • 88