23

In my case, the toolbar disappears when I scroll through the list. I am using a CollapsingToolbarLayout and I need to set the title text. But in my case the title text is not showing, even though I've set it (See code below). What is wrong?

Layout code:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">

<android.support.design.widget.CoordinatorLayout
    android:id="@+id/coordinator"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

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

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsing"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:fitsSystemWindows="true"
            app:layout_scrollFlags="scroll|enterAlways">


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

            </android.support.v7.widget.Toolbar>

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

    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

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

<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    app:headerLayout="@layout/app_nav_header_main"
    app:menu="@menu/main_drawer" />

</android.support.v4.widget.DrawerLayout>

Activity code:

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.app_activity_with_left_panel);

    mPreferences = PreferenceManager.getDefaultSharedPreferences(this);
    mToolbar = (Toolbar) findViewById(R.id.toolbar);
    mCollapsingToolbarLayout = (CollapsingToolbarLayout)findViewById(R.id.collapsing);
    setSupportActionBar(mToolbar);
    setTitle(getIntent().getStringExtra(TITLE));
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setHomeButtonEnabled(true);
}

@Override
public void setTitle(CharSequence title) {
    if (title != null && !title.toString().isEmpty()) {
        mTitle = title.toString();
        mCollapsingToolbarLayout.setTitle(mTitle);
    }
}
Patrick Mevzek
  • 10,995
  • 16
  • 38
  • 54
GPPSoft
  • 480
  • 1
  • 6
  • 15

5 Answers5

41

Remove this

@Override
public void setTitle(CharSequence title) {
    if (title != null && !title.toString().isEmpty()) {
        mTitle = title.toString();
        mCollapsingToolbarLayout.setTitle(mTitle);
    }
}

and add this on your OnCreate().

    mCollapsingToolbarLayout = (CollapsingToolbarLayout)findViewById(R.id.collapsing);
    mCollapsingToolbarLayout.setTitleEnabled(false);

    mToolbar.setTitle("title");

This disables the default title with collapsing behaviour and adds the static title to the toolbar.

Harshad Pansuriya
  • 20,189
  • 8
  • 67
  • 95
  • @GPPSoft see my answer. – Harshad Pansuriya May 20 '16 at 04:58
  • @Ironman Thx. Now it works. But not setting eny title text. Show default app name. :( mCollapsingToolbarLayout.setTitle("title"); not change title. – GPPSoft May 20 '16 at 05:04
  • @Ironman Still show default app name title. I try to change title< but not changed. – GPPSoft May 20 '16 at 05:09
  • @Ironman Sorry. This is works. It does not change the header when the application starts(show default appname). But if you change it when you click on button it works. Why is that? – GPPSoft May 20 '16 at 05:16
  • @GPPSoft is your problem solved ? app would show the name of defualt app as it starts it take app name from manifest i.e defined in string/app_name , and you're using shared preferences to set title so first you have to make some click actions so that shared preferences get something it and then you can set title. – mfaisalhyder May 20 '16 at 05:21
  • @Ironman Now it works! I changed to getSupportActionBar().setTitle(...); and title changed. But how set title on CollapsingToolbarLayout in my case&? Why mCollapsingToolbarLayout.setTitle() not works? Thx! – GPPSoft May 20 '16 at 05:25
  • @GPPSoft this `mCollapsingToolbarLayout.setTitle() ` not work because we set `mCollapsingToolbarLayout.setTitleEnabled(false);` to false.we can only set it when this `mCollapsingToolbarLayout.setTitleEnabled(true)` is true. – Harshad Pansuriya May 20 '16 at 05:29
  • @Ironman i set mCollapsingToolbarLayout.setTitleEnabled(true), but not works. – GPPSoft May 20 '16 at 05:35
  • @GPPSoft read this https://developer.android.com/reference/android/support/design/widget/CollapsingToolbarLayout.html – Harshad Pansuriya May 20 '16 at 05:39
  • @GPPSoft it is work but you have set it for `Expand` and `Collapse` both for that. – Harshad Pansuriya May 20 '16 at 05:40
16

For who searches for disabling the title just add

app:titleEnabled="false"

Then the title of the toolbar itself would appear so we will disable it with

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);

or with those line in the style xml

<item name="android:displayOptions">showHome|useLogo</item>
<item name="displayOptions">showHome|useLogo</item>

Searched too much to make this search summary, wish it helps.

Dasser Basyouni
  • 3,142
  • 5
  • 26
  • 50
3

In my case the problem was due to setting Toolbar layout_height to "wrap_content". After setting this property to a specific value like ?attr/actionBarSize, CollapsingToolbarLayout title become visible.

David Buck
  • 3,752
  • 35
  • 31
  • 35
mharam
  • 29
  • 1
  • 3
2

The above answers are correct, but it took me multiple tries to get it right. I ended up setting app:titleEnabled="false" for my CollapsingToolbarLayout in the xml, setting nothing for the Toolbar itself.

In the code, I set supportActionBar.setDisplayShowTitleEnabled(true), and supportActionBar.title = "my title".


My code:

layout.xml:

<com.google.android.material.appbar.CollapsingToolbarLayout
    android:id="@+id/fragment_main_toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:toolbarId="@id/toolbar"
    app:titleEnabled="false">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        app:menu="@menu/menu_main" />

</com.google.android.material.appbar.CollapsingToolbarLayout>

fragment.kt:

val toolbar = view.findViewById<androidx.appcompat.widget.Toolbar>(R.id.toolbar)
myTitle = "This is my app title"

(activity as AppCompatActivity).let { myActivity ->
    myActivity.setSupportActionBar(toolbar)
    myActivity.supportActionBar?.setDisplayShowTitleEnabled(true)
    myActivity.supportActionBar?.title = myTitle 

    // these are not needed:
    //collapsingToolbar.title = myTitle 
    //collapsingToolbar.isTitleEnabled = true
    //toolbar.title = myTitle 
    //myActivity.title = myTitle         
}
Cullub
  • 2,901
  • 3
  • 30
  • 47
1

use this :

app:expandedTitleTextAppearance="@android:color/transparent" with your CollapsingToolbarLayout

Vivek_Neel
  • 1,343
  • 1
  • 14
  • 25