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

androidx.appcompat.widget.toolbar example
android set toolbar title from fragment
toolbar not showing android
material toolbar'' android
add icon to toolbar android
android toolbar navigation icon
add back button to toolbar android
android action bar

I'm trying to create simple application using android-support-v7:21 library. Code snippets: MainActivity.java

public class MainActivity extends ActionBarActivity {

    Toolbar mActionBarToolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

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

activity_main.xml

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    android:orientation="vertical">

    <android.support.v7.widget.Toolbar            
        android:id="@+id/toolbar_actionbar"
        android:background="@null"
        android:layout_width="match_parent"
        android:layout_height="?actionBarSize"
        android:fitsSystemWindows="true" />

</LinearLayout>

But instead of "My title" on Toolbar %application name% is shown. Seems like setTitle method has no effect. I would like to show "My title".

UPD: Before, styles.xml was:

<style name="AppTheme" parent="Theme.AppCompat">
    <item name="windowActionBar">false</item>
</style>

So, I thought that actionbar is not used. I add NoActionBar to style parent:

<style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
    <item name="windowActionBar">false</item>
</style>

But the problem is not resolved.

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.

android.app.Activity.setTitle java code examples, In android app Toolbar.setTitle method has no effect – application name is shown as title. Activity activity = this.getActivity(); Toolbar toolbar = (Toolbar) activity. In android app Toolbar.setTitle method has no effect – application name is shown as title And toolbar is the general form of action bar. We can have multiple toolbars as layout widget but action is not. Thus better approach is to use getSupportActionBar().setTitle("My Title");

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);

android.widget.Toolbar, android.text.method The use of application icon plus title as a standard layout is discouraged on API 21 devices and newer. item click events if the items themselves do not have individual item click listeners. a view should have sound effects enabled for events such as clicking and touching. void, setTitle(​int resId). working - In android app Toolbar.setTitle method has no effect – application name is shown as title toolbar title not showing android (16) If you are using CollapsibleToolbarLayout along with Toolbar then you will need to set title in both the layouts set your Toolbar as action bar in onCreate method

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.

Set up the app bar, Add a Toolbar to an Activity; Use App Bar Utility Methods level 11), all activities that use the default theme have an ActionBar as an app bar. The problem is that only one time this mechanism works: once set value and collapse the toolbar, when I get down again the toolbar, set the text input value and collapse the toolbar, the title is not setted, that is, setTitle has no effect over the first time.

Simply you can change any acitivity name by using

Acitivityname.this.setTitle("Title NAme");

ActionBar SetTitle Not Working With Collapsing ToolBar [duplicate , But instead of "My title" on Toolbar %application name% is shown. Seems like setTitle method has no effect. I would like to show "My title". UPD: Before  The following are Jave code examples for showing how to use setTitle() of the android.support.v7.widget.Toolbar class. You can vote up the examples you like. You can vote up the examples you like. Your votes will be used in our system to get more good examples.

Try this, you can define title directly in XML:

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

In android app Toolbar.setTitle method has no effect, In android app Toolbar. I'm trying to create simple application using android-​support-v7:21 library Seems like setTitle method has no effect. What you're doing is correct. Fragments don't have access to the ActionBar APIs, so you have to call getActivity.Unless your Fragment is a static inner class, in which case you should create a WeakReference to the parent and call Activity.getActionBar from there.

I'm trying to create simple application using android-support-v7:21 library.Code snippets:MainActivity.javapublic class MainActivity extends ActionBarActivity  Android Toolbar Example. To learn to implement Toolbar, we need an Android Project. And for this example, let’s create a new one. Assuming you also have a new Android Project, here I have a project created using Android Studio 2.3. And you don’t need to add additional libraries if you are using version ahead 2.3. Changing Theme

In android app Toolbar.setTitle method has no effect – application name is shown as title. I'm trying to create simple application using android-support-v7:21  If this Toolbar does not have an expanded action view this method has no effect. An action view may be expanded either directly from the MenuItem it belongs to or by user action.

I'm trying to create simple application using android-support-v7:21 library. Code snippets: MainActivity.java public class MainActivity extends ActionBarActivity  ToolBar Tutorial With Example In Android Studio. In Android Toolbar is similar to an ActionBar(now called as App Bars). Toolbar is a Viewgroup that can be placed at anywhere in the Layout. We can easily replace an ActionBar with Toolbar.

Comments
  • FWIW, I'm on api level 22, and I had the same issue if calling setTitle after calling setSupportActionBar, but it worked if calling setTitle before. As described in an answer below, after calling setSupportActionBar, the action bar takes ownership of the toolbar and calls directly on the toolbar might not work. I do realize that in your question you did in fact call setTitle before setSupportActionBar, so probably this is something that has changed recently. In any case, using getSupportActionBar().setTitle works just as well.
  • The same query asked in stackoverflow.com/questions/15560904/…
  • Try it - "How set a title for Activity with ToolBar?" link.medium.com/cGVg3zVr0Z
  • nice one, but makes no sense to work this way and not the other....welcome to android 101
  • getSupportActionBar().setTitle("My title"); may produce 'java.lang.NullPointerException'
  • @PratikButani So long as the Toolbar has been set as setSupportActionBar() before calling getSupportActionBar() why might it?
  • Aha, easily resolved: assert getSupportActionBar() != null;
  • Guys did you really upvoted @PratikButani comment so many times? If you use style theme without action bar but custom support toolbar v7 and you forget to call setSupportActionBar(mToolbar) of course you will get null exeption, but if you called it then just ignore this message. Just read - developer.android.com/training/appbar/setting-up.html
  • It works, but why this is the way it must be done makes no sense to me. Does anyone have an explanation for why this works?
  • @Maxwell It makes sense considering the design of the actionbar/toolbar interaction. The Activity should display a title, if you provide a toolbar without that title, android assumes you want the traditional Activity title control.