Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android SupportActionBar does not refresh title

I have a problem with refreshing ActionBar title. The application is rather simple, currently it only have one activity: `

    <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">

            <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_count_scrolling"/>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fabReset"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/fab_margin"
        android:src="@android:drawable/ic_dialog_alert"
        app:layout_anchor="@id/app_bar"
        app:layout_anchorGravity="bottom|end"/>

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

The layout content_count_scrolling contains only NestedScrollView with RecyclerView inside.

My case is, when I enter a digit into the EditText in any of RecyclerView's row, it sets a value in the data model as well as recalculate the total value (sum from all rows). This total value should be set as ActionBar.title. To do this I am also using RxBus. MainActivity as below:

public class MainActivity extends AppCompatActivity {
    // ....
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_scrolling);
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        toolbar.setTitle("");

        mRxBus = getRxBusSingleton();        
        // ....
    }
}

public void refreshActionBarTitle() {
    String total = Data.getTotal();
    Timber.d("Refresh bar title with value="+total);

    toolbar.setTitle("");
    setSupportActionBar(toolbar);
    toolbar.setTitle("Title "+total);

    // testes also with:
    // getSupportActionBar().setTitle("Title "+total);

    Timber.d("Title after refresh: " + getSupportActionBar().getTitle());
}

`

Method refreshActionBarTitle() is triggered by RxBus. Once when method is triggered, the title of the ActionBar also is set (checked with logs and debugger). The issue is, that ActionBar is not invalidated and redrawn. This is done only after rotate the screen, which is obvious.

So, please help me with correct invalidate the toolbar. I have to notice also, that this event will be triggered after avery change in EditText, not only after changing the focus, so i cannot invalidate eg. whole screen.

I have checked also advices from Toolbar (SupportActionBar) title changes to app name on orientation change, but they did not help me.

like image 578
Dzinek Avatar asked Jul 30 '16 09:07

Dzinek


Video Answer


1 Answers

Fixed. I should use CollapsingToolbarLayout.setTitle() method. This one works

like image 165
Dzinek Avatar answered Oct 02 '22 04:10

Dzinek