In android app Toolbar.setTitle method has no effect – application name is shown as title In android app Toolbar.setTitle method has no effect – application name is shown as title android android

In android app Toolbar.setTitle method has no effect – application name is shown as title


Found the solution:

Instead of:

mActionBarToolbar = (Toolbar) findViewById(R.id.toolbar_actionbar);            mActionBarToolbar.setTitle("My title");setSupportActionBar(mActionBarToolbar);

I used:

mActionBarToolbar = (Toolbar) findViewById(R.id.toolbar_actionbar);            setSupportActionBar(mActionBarToolbar);getSupportActionBar().setTitle("My title");

And it works.


For anyone who needs to set up the title through the Toolbar some time after setting the SupportActionBar, read this.

The internal implementation of the support library just checks if the Toolbar has a title (not null) at the moment the SupportActionBar is set up. If there is, then this title will be used instead of the window title. You can then set a dummy title while you load the real title.

mActionBarToolbar = (Toolbar) findViewById(R.id.toolbar_actionbar);mActionBarToolbar.setTitle("");setSupportActionBar(mActionBarToolbar);

later...

mActionBarToolbar.setTitle(title);


The above answer is totally true but not working for me.

I solved my problem with the following things.

Actually My XML is like that:

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    android:id="@+id/confirm_order_mail_layout"    android:layout_width="match_parent"    android:layout_height="fill_parent">    <android.support.design.widget.AppBarLayout        android:id="@+id/confirm_order_appbar_layout"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">        <android.support.design.widget.CollapsingToolbarLayout            android:id="@+id/confirm_order_list_collapsing_toolbar"            android:layout_width="match_parent"            android:layout_height="match_parent"            android:fitsSystemWindows="true"            app:contentScrim="?attr/colorPrimary"            app:expandedTitleMarginEnd="64dp"            app:expandedTitleMarginStart="48dp"            app:layout_scrollFlags="scroll|enterAlways">            <android.support.v7.widget.Toolbar                android:id="@+id/confirm_order_toolbar_layout"                android:layout_width="match_parent"                android:layout_height="?attr/actionBarSize"                android:background="?attr/colorPrimary"                app:layout_scrollFlags="scroll|enterAlways"                app:popupTheme="@style/ThemeOverlay.AppCompat.Light">            </android.support.v7.widget.Toolbar>        </android.support.design.widget.CollapsingToolbarLayout>    </android.support.design.widget.AppBarLayout></android.support.design.widget.CoordinatorLayout>

I have tried all the option and after all I just removed CollapsingToolbarLayout because of i do not need to use in that particular XML So My Final XML is like:

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    android:id="@+id/confirm_order_mail_layout"    android:layout_width="match_parent"    android:layout_height="fill_parent">    <android.support.design.widget.AppBarLayout        android:id="@+id/confirm_order_appbar_layout"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">        <android.support.v7.widget.Toolbar            android:id="@+id/confirm_order_toolbar_layout"            android:layout_width="match_parent"            android:layout_height="?attr/actionBarSize"            android:background="?attr/colorPrimary"            app:layout_scrollFlags="scroll|enterAlways"            app:popupTheme="@style/ThemeOverlay.AppCompat.Light">        </android.support.v7.widget.Toolbar>    </android.support.design.widget.AppBarLayout></android.support.design.widget.CoordinatorLayout>

Now you have to use setTitle() like above answer:

mActionBarToolbar = (Toolbar) findViewById(R.id.confirm_order_toolbar_layout);            setSupportActionBar(mActionBarToolbar);getSupportActionBar().setTitle("My Title");

Now If you want to use CollapsingToolbarLayout and Toolbar together then you have to use setTitle() of CollapsingToolbarLayout

CollapsingToolbarLayout collapsingToolbarLayout = (CollapsingToolbarLayout) findViewById(R.id.confirm_order_mail_layout);collapsingToolbarLayout.setTitle("My Title");

May it will helps you. Thank you.