Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set scrim color programmatically

I'm trying to set the AppBarLayout's primary color programmatically. The XML layout is AndroidStudio's Scrolling sample:

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

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

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

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

And in the activity, I want all items inside the AppBarLayout to have a yellow background, so I'm setting:

int barColor = Color.parseColor("#FFC107");
AppBarLayout barLayout = (AppBarLayout) this.findViewById(R.id.app_bar);
if (barLayout != null) {
    barLayout.setBackgroundColor(barColor);
}
toolbar.setBackgroundColor(barColor);

CollapsingToolbarLayout collapsingToolbarLayout = (CollapsingToolbarLayout) this.findViewById(R.id.toolbar_layout);
if (collapsingToolbarLayout != null) {
    collapsingToolbarLayout.setBackgroundColor(barColor);
    collapsingToolbarLayout.setContentScrimColor(barColor);
}

Everything works fine, except when I'm halfway through scrolling the toolbar (in the exact point where the FAB disappears). In that state, the toolbar's color is still the default primary color (blue, not yellow), like in this image:

enter image description here

So, two questions:

  • Am I missing a method call?
  • Any tips on debugging these scenarios? In Android Device Monitor's view hierarchy dump I can't tell which one is the view that's tinted with this color.
like image 507
jlhonora Avatar asked Nov 13 '15 17:11

jlhonora


1 Answers

I was having the same problem, you have to set the statusBar scrim color as well:

    int red = ContextCompat.getColor(activity, R.color.red);
    collapsingToolbar.setBackgroundColor(red);
    collapsingToolbar.setContentScrimColor(red);
    collapsingToolbar.setStatusBarScrimColor(red);

you can even get the color directly using:

    collapsingToolbar.setBackgroundResource(R.color.red);
    collapsingToolbar.setContentScrimResource(R.color.red);
    collapsingToolbar.setStatusBarScrimResource(R.color.red);
like image 54
Marcio Granzotto Avatar answered Nov 15 '22 19:11

Marcio Granzotto