Set program color

I am trying to set the main color of AppBarLayout programmatically. XML Layout - AndroidStudio 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 this operation, I want all the elements inside the AppBarLayout to have a yellow background, so I set:

 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 scroll halfway through the toolbar (where FAB disappears). In this state, the color of the toolbar is still the default primary color (blue, not yellow), as in this image:

enter image description here

So, two questions:

  • Am I missing a method call?
  • Any tips on debugging these scripts? In the dump of the Android Device Monitor view hierarchy, I can’t determine which one is the tone colored by this color.
+5
source share
1 answer

I had the same problem, you should also set the color to a scrim statusBar:

  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); 
+13
source

Source: https://habr.com/ru/post/1235903/


All Articles