Created
January 24, 2022 16:46
-
-
Save gcantoni/f96f14f8497113e398814246c25961d9 to your computer and use it in GitHub Desktop.
Status Bar Scroll Surface - Material You
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @author Giorgio Cantoni - [email protected] | |
*/ | |
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