Skip to content

Instantly share code, notes, and snippets.

@gcantoni
Created January 24, 2022 16:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gcantoni/f96f14f8497113e398814246c25961d9 to your computer and use it in GitHub Desktop.
Save gcantoni/f96f14f8497113e398814246c25961d9 to your computer and use it in GitHub Desktop.
Status Bar Scroll Surface - Material You
/**
* @author Giorgio Cantoni - giorgio.canto98@gmail.com
*/
public class StatusBarScrollSurface {
/**
* Make the color of the status bar the same as the surface color of the toolbar when scrolling
* @param context The current context
* @param appBarLayout The current appBarLayout
* @param toolbar The current toolbar
* @param window The current window
*/
public static void enableStatusBarScrollSurface(Context context, AppBarLayout appBarLayout, androidx.appcompat.widget.Toolbar toolbar, Window window) {
final int[] scrollRange = {-1};
appBarLayout.addOnOffsetChangedListener((appBarLayout1, verticalOffset) -> {
// initialize the size of the scroll
if (scrollRange[0] == -1) {
scrollRange[0] = appBarLayout.getTotalScrollRange();
}
// check if the view is collapsed
if (scrollRange[0] + verticalOffset == 0) {
toolbar.setBackgroundColor(ContextCompat.getColor(context, R.color.colorPrimarySurface));
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(ContextCompat.getColor(context, R.color.colorPrimarySurface));
} else {
toolbar.setBackgroundColor(ContextCompat.getColor(context, android.R.color.transparent));
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(ContextCompat.getColor(context, android.R.color.transparent));
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment