Search code examples
androidandroid-actionbartitle

Change the title of my action bar


I'm trying to change the title of the action bar when returning from another activity.

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    setSupportActionBar(toolbar);
    Log.d("TOOLBAR", getSupportActionBar().getTitle().toString());
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setTitle("title 2");
    Log.d("TOOLBAR 2", getSupportActionBar().getTitle().toString());
    }

where in the onCreate method:

toolbar = findViewById(R.id.toolbar);

If for example my previous title was "title 1", I can read these logs:

TOOLBAR: title
TOOLBAR 2: title 2

but in the emulator the title isn't changed. I tried also to change the title of toolbar.

EDIT:
This is my activity:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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="cct.sss.ViewRecordActivity">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar"
        android:layout_width="match_parent"
        android:layout_height="@dimen/app_bar_height"
        android:fitsSystemWindows="true"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/toolbar_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            app:toolbarId="@+id/toolbar">

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

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

    <include layout="@layout/content_view_record" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/view_record_fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/fab_margin"
        android:onClick="editRecord"
        android:tint="@color/white"
        app:layout_anchor="@id/app_bar"
        app:layout_anchorGravity="bottom|end"
        app:srcCompat="@drawable/ic_edit_black_24dp" />

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

Solution

  • Thank you all for your answers. I solved my problem simply setting the title of the collapsing toolbar layout in onActivityResult():

    collapsingToolbarLayout.setTitle("title");