Original: https://developer.android.com/training/system-ui/index.html

Management System UI

System bars are areas dedicated to displaying alerts and messages for devices. It is usually displayed at the top of the screen, but users can temporarily dim the system bar to make it more focused when using immersive apps such as videos and photos, or hide it to achieve a fully immersive experience.

If you’re familiar with Android’s design patterns, you know the importance of conforming to standard design specifications. You need to make changes to the status bar based on the user experience.

This lesson describes how to dim and hide the status bar in different versions to provide an immersive experience while keeping it easy to call up the status bar.

course

Dim system column

Learn how to dim the status bar and navigation bar. Support android4.0 or above.

Dim the status bar and navigation bar

Use the view. SYSTEM_UI_FLAG_LOW_PROFILE flag.

// This example uses decor view, but you can use any visible view.
View decorView = getActivity().getWindow().getDecorView();
int uiOptions = View.SYSTEM_UI_FLAG_LOW_PROFILE;
decorView.setSystemUiVisibility(uiOptions);
Copy the code

The status bar is fully displayed when you call up the navigation bar. If you want to dim it again, you have to set it again.

Fully display the status bar and navigation bar

View decorView = getActivity().getWindow().getDecorView();
// Calling setSystemUiVisibility() with a value of 0 clears
// all flags.
decorView.setSystemUiVisibility(0);
Copy the code

Hide status bar

4.0 and below

Method 1: Modify the manifest file

<application
    .
    android:theme="@android:style/Theme.Holo.NoActionBar.Fullscreen" >.</application>
Copy the code

The advantages of using an activity theme are as follows:

  • It’s easier to maintain and less error-prone than setting a flag programmatically.
  • It results in smoother UI transitions, because the system has the information it needs to render your UI before instantiating your app’s main activity.

Method 2: Modify WindowManager Flags

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // If the Android version is lower than Jellybean, use this call to hide
        // the status bar.
        if (Build.VERSION.SDK_INT < 16) { getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); } setContentView(R.layout.activity_main); }... }Copy the code

When you set WindowManager flags (whether through an activity theme or programmatically), the flags remain in effect unless your app clears them.

You can use FLAG_LAYOUT_IN_SCREEN to set your activity layout to use the same screen area that’s available when you’ve enabled FLAG_FULLSCREEN. This prevents your content from resizing when the status bar hides and shows.

4.1 and above

Using setSystemUiVisibility() is more verbose than changing WindowManager Flags.

View decorView = getWindow().getDecorView();
// Hide the status bar.
int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN;
decorView.setSystemUiVisibility(uiOptions);
// Remember that you should never show the action bar if the
// status bar is hidden, so hide that too if necessary.
ActionBar actionBar = getActionBar();
actionBar.hide();
Copy the code

Hide navigation

Use full-screen immersion mode

Android 4.4 (API Level 19) adds a new UI flag, SYSTEM_UI_FLAG_IMMERSIVE, to go truly full-screen. When this flag is combined with SYSTEM_UI_FLAG_HIDE_NAVIGATION and SYSTEM_UI_FLAG_FULLSCREEN, the status bar is hidden and all touch events are captured.

When immersive mode is enabled, all touch events are still received. Users can swipe to the status bar and navigation bar in beta. This action clears SYSTEM_UI_FLAG_HIDE_NAVIGATION flag (and SYSTEM_UI_FLAG_FULLSCREEN if set) so that the system bar can be displayed. This operation will trigger the OnSystemUiVisivilityChangeListener. But if you want the system bar to be hidden automatically after a certain period of time, use SYSTEM_UI_FLAG_IMMERSIVE_STICKY. Note that “sticky” does not trigger any listening.

Respond to changes in UI view visibility

So how do you respond to a status bar change? If you want to View other sync status bar hidden and disappear, also can use the OnSystemUiVisibilityChangeListener.

For example, you can add in onCreate:

View decorView = getWindow().getDecorView();
decorView.setOnSystemUiVisibilityChangeListener
        (new View.OnSystemUiVisibilityChangeListener() {
    @Override
    public void onSystemUiVisibilityChange(int visibility) {
        // Note that system bars will only be "visible" if none of the
        // LOW_PROFILE, HIDE_NAVIGATION, or FULLSCREEN flags are set.
        if ((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0) {
            // TODO: The system bars are visible. Make any desired
            // adjustments to your UI, such as showing the action bar or
            // other navigational controls.
        } else {
            // TODO: The system bars are NOT visible. Make any desired
            // adjustments to your UI, such as hiding the action bar or
            // other navigational controls.}}});Copy the code

Using this listener to synchronize the entire interface is beneficial for overall interface consistency.