Skip to content

Most visited

Recently visited

navigation
ImmersiveMode / src / com.example.android.immersivemode /

ImmersiveModeFragment.java

1
/*
2
* Copyright (C) 2012 The Android Open Source Project
3
*
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
7
*
8
*      http://www.apache.org/licenses/LICENSE-2.0
9
*
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
15
*/
16
package com.example.android.immersivemode;
17
 
18
import android.os.Build;
19
import android.os.Bundle;
20
import android.support.v4.app.Fragment;
21
import android.view.MenuItem;
22
import android.view.View;
23
 
24
import com.example.android.common.logger.Log;
25
 
26
public class ImmersiveModeFragment extends Fragment {
27
 
28
    public static final String TAG = "ImmersiveModeFragment";
29
 
30
    @Override
31
    public void onCreate(Bundle savedInstanceState) {
32
        super.onCreate(savedInstanceState);
33
        setHasOptionsMenu(true);
34
    }
35
 
36
    @Override
37
    public void onActivityCreated(Bundle savedInstanceState) {
38
        super.onActivityCreated(savedInstanceState);
39
        final View decorView = getActivity().getWindow().getDecorView();
40
        decorView.setOnSystemUiVisibilityChangeListener(
41
                new View.OnSystemUiVisibilityChangeListener() {
42
                    @Override
43
                    public void onSystemUiVisibilityChange(int i) {
44
                        int height = decorView.getHeight();
45
                        Log.i(TAG, "Current height: " + height);
46
                    }
47
                });
48
    }
49
 
50
    @Override
51
    public boolean onOptionsItemSelected(MenuItem item) {
52
        if (item.getItemId() == R.id.sample_action) {
53
            toggleHideyBar();
54
        }
55
        return true;
56
    }
57
 
58
    /**
59
     * Detects and toggles immersive mode (also known as "hidey bar" mode).
60
     */
61
    public void toggleHideyBar() {
62
 
64
        // The UI options currently enabled are represented by a bitfield.
65
        // getSystemUiVisibility() gives us that bitfield.
66
        int uiOptions = getActivity().getWindow().getDecorView().getSystemUiVisibility();
67
        int newUiOptions = uiOptions;
70
        boolean isImmersiveModeEnabled =
71
                ((uiOptions | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY) == uiOptions);
72
        if (isImmersiveModeEnabled) {
73
            Log.i(TAG, "Turning immersive mode mode off. ");
74
        } else {
75
            Log.i(TAG, "Turning immersive mode mode on.");
76
        }
77
 
78
        // Navigation bar hiding:  Backwards compatible to ICS.
79
        if (Build.VERSION.SDK_INT >= 14) {
80
            newUiOptions ^= View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
81
        }
82
 
83
        // Status bar hiding: Backwards compatible to Jellybean
84
        if (Build.VERSION.SDK_INT >= 16) {
85
            newUiOptions ^= View.SYSTEM_UI_FLAG_FULLSCREEN;
86
        }
87
 
88
        // Immersive mode: Backward compatible to KitKat.
89
        // Note that this flag doesn't do anything by itself, it only augments the behavior
90
        // of HIDE_NAVIGATION and FLAG_FULLSCREEN.  For the purposes of this sample
91
        // all three flags are being toggled together.
92
        // Note that there are two immersive mode UI flags, one of which is referred to as "sticky".
93
        // Sticky immersive mode differs in that it makes the navigation and status bars
94
        // semi-transparent, and the UI flag does not get cleared when the user interacts with
95
        // the screen.
96
        if (Build.VERSION.SDK_INT >= 18) {
97
            newUiOptions ^= View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
98
        }
99
 
100
        getActivity().getWindow().getDecorView().setSystemUiVisibility(newUiOptions);
102
    }
103
}
This site uses cookies to store your preferences for site-specific language and display options.

Hooray!

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.

Take a one-minute survey?
Help us improve Android tools and documentation.