How To Show Content Behind The Status Bar?

Many times you end up with tasks in which you have to show app content behind the status bar.
Then you search for the solution on web,copy and paste code from many websites and blogs but you don't get the result you want.
In this simple tutorial we are gonna learn how to show app content behind the status bar.

Figure 1. Show image behind the status bar.
Figure 1. Image behind status bar.

In this approach your content does not resize when status bar show or hide,So you get consistence user experience.
You can show app content behind the status bar using SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN You should also use SYSTEM_UI_FLAG_LAYOUT_STABLE to help your app maintain stable layout.

And remember to set the transparent status bar either in style.xml
<item name="android:statusBarColor">@android:color/transparent</item>
Or set the transparent status bar programmatically:
getWindow().setStatusBarColor(Color.TRANSPARENT) 

As show in following code snipped.


When you use this approach you should careful about your app's UI critical parts ( Action Bar , Default Map Controls etc) are not overlap by system windows.In many cases you can handle this issue by adding android:fitsSystemWindows attribute in your layout xml file with the value set to true.This attribute set the proper padding to parent viewGroup so that you app content draw after that padding and do not overlap by system windows.

For many cases android:fitsSystemWindows works perfectly,but if you want to alter default padding to get your disired layout.You can set public void setOnApplyWindowInsetsListener(View.OnApplyWindowInsetsListener listener)
By passing object of View.OnApplyWindowInsetsListener. Space which occupy by system bars are also know as window insets. Listener will call when system set the window insets you can then use those window insets as your needs.

Official Documentation Says "Set an OnApplyWindowInsetsListener to take over the policy for applying window insets to this view. The listener's OnApplyWindowInsetsListener#onApplyWindowInsets(View, WindowInsets) method will be called instead of the view's onApplyWindowInsets method."

As show in following code snipped.

 


No comments:

Post a Comment