Skip to content

Instantly share code, notes, and snippets.

@Anton111111
Created February 4, 2019 12:45
Show Gist options
  • Select an option

  • Save Anton111111/89ba4e82bfe015f7d99df24862ad309b to your computer and use it in GitHub Desktop.

Select an option

Save Anton111111/89ba4e82bfe015f7d99df24862ad309b to your computer and use it in GitHub Desktop.
Transparent statusbar
/**
* need call setFitsSystemWindows(false) on view that need it
*
* @param activity
*/
public static void setStatusBarTransparent(@NonNull Activity activity) {
if (Build.VERSION.SDK_INT >= 19 && Build.VERSION.SDK_INT < 21) {
setWindowFlag(activity, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, true);
}
if (Build.VERSION.SDK_INT >= 19) {
activity.getWindow().getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE |
View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN |
View.SYSTEM_UI_FLAG_IMMERSIVE);
}
if (Build.VERSION.SDK_INT >= 21) {
activity.getWindow().setStatusBarColor(Color.TRANSPARENT);
}
ViewGroup layoutRoot = getLayoutRoot(activity);
if (layoutRoot != null) {
layoutRoot.setFitsSystemWindows(false);
}
}
/**
* Make status bar with solid color
* need call setFitsSystemWindows(true) on view that need it
*
* @param activity
* @param statusBarColor
*/
public static void setStatusBarColor(@NonNull Activity activity, int statusBarColor) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
activity.getWindow().setStatusBarColor(statusBarColor);
}
activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
ViewGroup layoutRoot = getLayoutRoot(activity);
if (layoutRoot != null) {
layoutRoot.setFitsSystemWindows(true);
}
}
public static ViewGroup getLayoutRoot(Activity activity) {
ViewGroup contentView = activity.findViewById(android.R.id.content);
View maybelayoutRoot = contentView.getChildAt(0);
if (maybelayoutRoot != null && maybelayoutRoot instanceof ViewGroup) {
return (ViewGroup) maybelayoutRoot;
}
return null;
}
public static void setWindowFlag(Activity activity, final int bits, boolean on) {
Window win = activity.getWindow();
WindowManager.LayoutParams winParams = win.getAttributes();
if (on) {
winParams.flags |= bits;
} else {
winParams.flags &= ~bits;
}
win.setAttributes(winParams);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment