This article is more a summary of other people’s learning achievements, if offended please contact me immediately delete.

Here’s a link to the article: Let your status bar change color (Hongyang) Android status bar tips, StatusBar Anroid immersive StatusBar Android full screen/immersive StatusBar, a variety of keyboard blocking input box solutions (guo linrecommended article)

1. As for why this status bar is always called “immersive top Bars”, many in China always call it. (I found myself Posting almost every post about Statusbar; 2. The immersive status bar is only available on systems above 4.4; And 4.4 to 5.0 to 5.0 above the situation here

System elements on Android

Hide the status bar and ActionBar

public class MainActivity extends AppCompatActivity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

View decorView = getWindow().getDecorView();

int option = View.SYSTEM_UI_FLAG_FULLSCREEN;

// Set the visibility of system UI elements. SYSTEM_UI_FLAG_FULLSCREEN means full screen, which means the status bar is hidden.

decorView.setSystemUiVisibility(option);

ActionBar actionBar = getSupportActionBar();

if(actionBar ! =null) {

// Hide the actionBar

actionBar.hide();

}

}

}

Copy the code

The above code effect is shown as follows:

Make the status bar transparent

// Only 5.0 or later is supported

if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){

View decorView = getWindow().getDecorView();

// The two flags must be used together to indicate that the main content of the application occupies space in the system status bar

int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN

| View.SYSTEM_UI_FLAG_LAYOUT_STABLE;

decorView.setSystemUiVisibility(option);

// Make the status bar transparent

getWindow().setStatusBarColor(Color.TRANSPARENT);

}

ActionBar actionBar = getSupportActionBar();

if(actionBar ! =null) {

// Hide the actionBar

actionBar.hide();

}

Copy the code

Effect:

The problem here is that if our app has a title bar (or toolbar); The title bar is partially obscured by the system’s status bar.

The diagram below:



The title bar partially overlaps the status bar. The solution is:

fitsSystemWindows=”true”

Set the ViewGroup under the ContentView to reserve space for the system View.

SetFitsSystemWindows properties

void setFitsSystemWindows (boolean fitSystemWindows)

Sets whether or not this view should account for system screen decorations such as the status bar and inset its content; that is, controlling whether the default implementation of fitSystemWindows(Rect) will be executed. See that method for more details.

Note that if you are providing your own implementation of fitSystemWindows(Rect), then there is no need to set this flag to true — your implementation will be overriding the default implementation that checks this flag.

SetFitsSystemWindows sets the toolbars that affect the system, such as the status bar, that determine whether the view is inserted into its ContentView. When you set fitSystemWindows(Rect) and do not set setFitsSystemWindows to True, your fitSystemWindows(Rect) Settings are invalid. This property sets whether space is reserved for the system View. When set to true, space is reserved for the status bar.

Another way to set the transparent status bar

 if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){

// You need to set this flag to call setStatusBarColor to set the status bar color

activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);

activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);

// Status bar transparent color

activity.getWindow().setStatusBarColor(Color.TRANSPARENT);

}

// To set the full screen

ViewGroup mContentView = (ViewGroup) activity.findViewById(android.R.id.content);

View mChildView = mContentView.getChildAt(0);

if(mChildView ! =null) {

// Instead of setting the FitsSystemWindows ContentView, set the first child of the ContentView. It does not reserve space for the system View.

ViewCompat.setFitsSystemWindows(mChildView, true);

}

Copy the code

Notice the last line of code:

ViewCompat.setFitsSystemWindows(mChildView, true);

This is equivalent to setFitsSystemWindows(true); , but the above makes downward compatibility.

If true, space is reserved for the status bar. Remember that! Remember that! Remember that!

You don’t need true if your layout is the entire image. False is also acceptable, and true is mainly used for layouts with title bars. Prevent the status bar from partially overlapping the title bar.

stickersA hint written by the Great God of Hong Xiang

This can be done if our layout background color is white

View decorView = getWindow().getDecorView();

// key: SYSTEM_UI_FLAG_LIGHT_STATUS_BAR

int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN |

View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;

decorView.setSystemUiVisibility(option);

getWindow().setStatusBarColor(Color.TRANSPARENT);

Copy the code

To sum up a few points

  • In full screen mode with a picture as the background and no title bar. You don’t need to set the setFitsSystemWindows property.
  • Full screen mode with picture as background and title bar. setFitsSystemWindows(false); No space for the status bar. And setClipToPadding (true); That means you can use padding to draw other views
  • The background is not a picture and there is a title bar. setFitsSystemWindows(true);

The above is based on how to make the status bar transparent above 5.0.

Between 4.4 and 5.0

This is done by adding a view of the same height as the status bar and setting the background color of the status bar

if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){

// Between 5.0 and 4.4

// Set the status bar transparency property

activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);

ViewGroup decorView = (ViewGroup)activity.getWindow().getDecorView();

int count = decorView.getChildCount();

// Create a status bar sized rectangle and add it to the layout

if(count > 0 && decorView.getChildAt(count - 1) instanceof StatusBarView){

// Check if the last decorView is a StatusBarView

decorView.getChildAt(count - 1).setBackgroundColor(color);

} else {

StatusBarView statusBarView = createStatusBarView(activity,color,statusBarAlpha);

// Add statusBarView to the end of the decorView; Since the decorView is FrameLayout, the statusBarView is placed at the top

decorView.addView(statusBarView);

}

}

/ * *

* Creates a translucent rectangle the same size as the status bar

* @paramActivity Specifies the activity to be set

* @paramColor Indicates the color of the status bar

* @paramAlpha transparency value

* @returnStatus bar rectangle bar

* /


private static StatusBarView createStatusBarView(Activity activity,@ColorInt int color,int alpha){

// Draw a rectangle the same height as the status bar StatusBarView is essentially a View; No properties at all

StatusBarView statusBarView = new StatusBarView(activity);

// Width is MATCH_PARENT and height is the height of the status bar

LinearLayout.LayoutParams params =

new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,getStatusBarHeight(activity));

statusBarView.setLayoutParams(params);

statusBarView.setBackgroundColor(color);

return statusBarView;

}

Copy the code

Status bar problem while editing status:

When using the immersive status bar, if the interface has an edit bar: The blog post recommended by Guo Lin helps you

Here’S the important part of Method 5:

Implementation steps:

1. Copy the SoftHideKeyBoardUtil class into the project. 2, added in the need to use the Activity’s onCreate method: SoftHideKeyBoardUtil. AssistActivity (this); Can.

/ * *

* Solve the keyboard blocking input box

* Created by SmileXie on 2017/4/3.

* /


public class SoftHideKeyBoardUtil {

public static void assistActivity (Activity activity) {

new SoftHideKeyBoardUtil(activity);

}

private View mChildOfContent;

private int usableHeightPrevious;

private FrameLayout.LayoutParams frameLayoutParams;

// In order to adapt to huawei and Xiaomi phones, black bars appear above the keyboard or do not fit

private int contentHeight;// Get the height of setContentView

private boolean isfirst = true;// Fetch only once

private int statusBarHeight;// Status bar height

private SoftHideKeyBoardUtil(Activity activity) {

// Find the Activity's outermost layout control. It is actually a DecorView. The control used is FrameLayout

FrameLayout content = (FrameLayout) activity.findViewById(android.R.id.content);

//2, get the View that setContentView put in

mChildOfContent = content.getChildAt(0);

//3. Set a View tree listener for the Activity's XML layout. This listener is called when the layout changes, such as when the keyboard pops up or collapses

mChildOfContent.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {

//4. When the soft keyboard is up, GlobalLayout changes

public void onGlobalLayout() {

if (isfirst) {

contentHeight = mChildOfContent.getHeight();// Compatible with Huawei and other models

isfirst = false;

}

// redraw the Activity's XML layout when the current layout changes

possiblyResizeChildOfContent();

}

});

// Get the Activity's XML layout placement parameters

frameLayoutParams = (FrameLayout.LayoutParams) mChildOfContent.getLayoutParams();

}

If the soft keyboard is up, subtract the keyboard height from the XML layout height of the Activity

private void possiblyResizeChildOfContent() {

//1. Obtain the available height of the current interface. After the keyboard is up, the available layout of the current interface will reduce the height of the keyboard

int usableHeightNow = computeUsableHeight();

//2. If the current available height is different from the original value

if(usableHeightNow ! = usableHeightPrevious) {

// get the height of the Activity's XML layout displayed on the current screen

int usableHeightSansKeyboard = mChildOfContent.getRootView().getHeight();

// The height of the XML layout in the Activity - the currently available height

int heightDifference = usableHeightSansKeyboard - usableHeightNow;

//5. When the height difference is greater than 1/4 of the screen, the keyboard pops up

if (heightDifference > (usableHeightSansKeyboard/4)) {

The height of the Activity's XML layout should be subtracted from the height of the keyboard

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){

frameLayoutParams.height = usableHeightSansKeyboard - heightDifference + statusBarHeight;

} else {

frameLayoutParams.height = usableHeightSansKeyboard - heightDifference;

}

} else {

frameLayoutParams.height = contentHeight;

}

// redraw the Activity's XML layout

mChildOfContent.requestLayout();

usableHeightPrevious = usableHeightNow;

}

}

private int computeUsableHeight() {

Rect r = new Rect();

mChildOfContent.getWindowVisibleDisplayFrame(r);

// In full screen mode, return r.box, which is the height of the status bar

return (r.bottom - r.top);

}

}

Copy the code

(Self-recorded attributes can be ignored)

Each Activity in the Manifest file can be set as shown below:

Truly immersive mode

The onWindowFocusChanged() method of an Activity overwritten in immersive mode is supported only on Android 4.4 and above

public class MainActivity extends AppCompatActivity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

}

@Override

public void onWindowFocusChanged(boolean hasFocus) {

super.onWindowFocusChanged(hasFocus);

if (hasFocus && Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {

View decorView = getWindow().getDecorView();

decorView.setSystemUiVisibility(

View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION

//SYSTEM_UI_FLAG_LAYOUT_STABLE when combined with SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN, the main content of an application occupies space in the system status bar

| View.SYSTEM_UI_FLAG_LAYOUT_STABLE

| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN

| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION

// Full screen means to hide the status bar

| View.SYSTEM_UI_FLAG_FULLSCREEN

| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);

}

}

}

Copy the code

The interface is full-screen by default, and the status bar and navigation bar are not displayed. When we need to use the status bar or navigation bar, we just need to pull down at the top of the screen, or pull left on the right side of the screen, the status bar and navigation bar will be displayed, and the display or size of any element on the interface will not be affected. After a while, the status bar and navigation bar will automatically hide and return to full screen if no action is taken.

Hide navigation

View decorView = getWindow().getDecorView();

// Hide the status bar and navigation bar at the same time

int option = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION

| View.SYSTEM_UI_FLAG_FULLSCREEN;

decorView.setSystemUiVisibility(option);

ActionBar actionBar = getSupportActionBar();

if(actionBar ! =null) {

// Hide the actionBar

actionBar.hide();

}

Copy the code

It may look like it’s finally full screen, but in this mode, touching anywhere on the screen exits full screen.

The effect is as follows:

Transparent status bar and navigation bar

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {

View decorView = getWindow().getDecorView();

//SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION, which allows the main content of the application to occupy space in the system navigation bar

int option = View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION

| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN

| View.SYSTEM_UI_FLAG_LAYOUT_STABLE;

decorView.setSystemUiVisibility(option);

The setNavigationBarColor() method sets the navigation bar to a transparent color

getWindow().setNavigationBarColor(Color.TRANSPARENT);

getWindow().setStatusBarColor(Color.TRANSPARENT);

}

ActionBar actionBar = getSupportActionBar();

if(actionBar ! =null) {

// Hide the actionBar

actionBar.hide();

}

Copy the code

Effect: