Created
February 4, 2019 12:45
-
-
Save Anton111111/89ba4e82bfe015f7d99df24862ad309b to your computer and use it in GitHub Desktop.
Transparent statusbar
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
| /** | |
| * 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