to top
AdvancedImmersiveMode / src / com.example.android.advancedimmersivemode /

AdvancedImmersiveModeFragment.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.advancedimmersivemode;
17
 
18
import android.os.Bundle;
19
import android.support.v4.app.Fragment;
20
import android.view.MenuItem;
21
import android.view.View;
22
import android.view.ViewGroup;
23
import android.widget.CheckBox;
24
 
25
import com.example.android.common.logger.Log;
26
 
27
/**
28
 * Demonstrates how to update the app's UI by toggling immersive mode.
29
 * Checkboxes are also made available for toggling other UI flags which can
30
 * alter the behavior of immersive mode.
31
 */
32
public class AdvancedImmersiveModeFragment extends Fragment {
33
 
34
    public static final String TAG = "AdvancedImmersiveModeFragment";
35
    public CheckBox mHideNavCheckbox;
36
    public CheckBox mHideStatusBarCheckBox;
37
    public CheckBox mImmersiveModeCheckBox;
38
    public CheckBox mImmersiveModeStickyCheckBox;
39
    public CheckBox mLowProfileCheckBox;
40
 
41
 
42
    @Override
43
    public void onCreate(Bundle savedInstanceState) {
44
        super.onCreate(savedInstanceState);
45
        setHasOptionsMenu(true);
46
    }
47
 
48
    @Override
49
    public void onActivityCreated(Bundle savedInstanceState) {
50
        super.onActivityCreated(savedInstanceState);
51
 
52
        final View decorView = getActivity().getWindow().getDecorView();
53
        ViewGroup parentView = (ViewGroup) getActivity().getWindow().getDecorView()
54
                .findViewById(R.id.sample_main_layout);
55
 
56
        mLowProfileCheckBox = new CheckBox(getActivity());
57
        mLowProfileCheckBox.setText("Enable Low Profile mode.");
58
        parentView.addView(mLowProfileCheckBox);
59
 
60
        mHideNavCheckbox = new CheckBox(getActivity());
61
        mHideNavCheckbox.setChecked(true);
62
        mHideNavCheckbox.setText("Hide Navigation bar");
63
        parentView.addView(mHideNavCheckbox);
64
 
65
        mHideStatusBarCheckBox = new CheckBox(getActivity());
66
        mHideStatusBarCheckBox.setChecked(true);
67
        mHideStatusBarCheckBox.setText("Hide Status Bar");
68
        parentView.addView(mHideStatusBarCheckBox);
69
 
70
        mImmersiveModeCheckBox = new CheckBox(getActivity());
71
        mImmersiveModeCheckBox.setText("Enable Immersive Mode.");
72
        parentView.addView(mImmersiveModeCheckBox);
73
 
74
        mImmersiveModeStickyCheckBox = new CheckBox(getActivity());
75
        mImmersiveModeStickyCheckBox.setText("Enable Immersive Mode (Sticky)");
76
        parentView.addView(mImmersiveModeStickyCheckBox);
77
 
78
    }
79
 
80
    @Override
81
    public boolean onOptionsItemSelected(MenuItem item) {
82
        if (item.getItemId() == R.id.sample_action) {
83
            toggleImmersiveMode();
84
        }
85
        return true;
86
    }
87
 
88
    /**
89
     * Detects and toggles immersive mode (also known as "hidey bar" mode).
90
     */
91
    public void toggleImmersiveMode() {
92
 
94
        // The "Decor View" is the parent view of the Activity.  It's also conveniently the easiest
95
        // one to find from within a fragment, since there's a handy helper method to pull it, and
96
        // we don't have to bother with picking a view somewhere deeper in the hierarchy and calling
97
        // "findViewById" on it.
98
        View decorView = getActivity().getWindow().getDecorView();
99
        int uiOptions = decorView.getSystemUiVisibility();
100
        int newUiOptions = uiOptions;
102
 
104
        // Low profile mode doesn't resize the screen at all, but it covers the nav & status bar
105
        // icons with black so they're less distracting.  Unlike "full screen" and "hide nav bar,"
106
        // this mode doesn't interact with immersive mode at all, but it's instructive when running
107
        // this sample to observe the differences in behavior.
108
        if (mLowProfileCheckBox.isChecked()) {
109
            newUiOptions |= View.SYSTEM_UI_FLAG_LOW_PROFILE;
110
        } else {
111
            newUiOptions &= ~View.SYSTEM_UI_FLAG_LOW_PROFILE;
112
        }
114
 
116
        // When enabled, this flag hides non-critical UI, such as the status bar,
117
        // which usually shows notification icons, battery life, etc
118
        // on phone-sized devices.  The bar reappears when the user swipes it down.  When immersive
119
        // mode is also enabled, the app-drawable area expands, and when the status bar is swiped
120
        // down, it appears semi-transparently and slides in over the app, instead of pushing it
121
        // down.
122
        if (mHideStatusBarCheckBox.isChecked()) {
123
            newUiOptions |= View.SYSTEM_UI_FLAG_FULLSCREEN;
124
        } else {
125
            newUiOptions &= ~View.SYSTEM_UI_FLAG_FULLSCREEN;
126
        }
128
 
130
        // When enabled, this flag hides the black nav bar along the bottom,
131
        // where the home/back buttons are.  The nav bar normally instantly reappears
132
        // when the user touches the screen.  When immersive mode is also enabled, the nav bar
133
        // stays hidden until the user swipes it back.
134
        if (mHideNavCheckbox.isChecked()) {
135
            newUiOptions |= View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
136
        } else {
137
            newUiOptions &= ~View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
138
        }
140
 
142
        // Immersive mode doesn't do anything without at least one of the previous flags
143
        // enabled.  When enabled, it allows the user to swipe the status and/or nav bars
144
        // off-screen.  When the user swipes the bars back onto the screen, the flags are cleared
145
        // and immersive mode is automatically disabled.
146
        if (mImmersiveModeCheckBox.isChecked()) {
147
            newUiOptions |= View.SYSTEM_UI_FLAG_IMMERSIVE;
148
        } else {
149
            newUiOptions &= ~View.SYSTEM_UI_FLAG_IMMERSIVE;
150
        }
152
 
154
        // There's actually two forms of immersive mode, normal and "sticky".  Sticky immersive mode
155
        // is different in 2 key ways:
156
        //
157
        // * Uses semi-transparent bars for the nav and status bars
158
        // * This UI flag will *not* be cleared when the user interacts with the UI.
159
        //   When the user swipes, the bars will temporarily appear for a few seconds and then
160
        //   disappear again.
161
        if (mImmersiveModeStickyCheckBox.isChecked()) {
162
            newUiOptions |= View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
163
        } else {
164
            newUiOptions &= ~View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
165
        }
167
 
169
        //Set the new UI flags.
170
        decorView.setSystemUiVisibility(newUiOptions);
171
        Log.i(TAG, "Current height: " + decorView.getHeight() + ", width: " + decorView.getWidth());
173
    }
174
}