Please note that the contents of this offline web site may be out of date. To access the most recent documentation visit the online version .
Note that links that point to online resources are green in color and will open in a new window.
We would love it if you could give us feedback about this material by filling this form (You have to be online to fill it)
ImmersiveMode / src / com.example.android.immersivemode /

ImmersiveModeFragment.java

      
        1
       
        2
       
        3
       
        4
       
        5
       
        6
       
        7
       
        8
       
        9
       
        10
       
        11
       
        12
       
        13
       
        14
       
        15
       
        16
       
        17
       
        18
       
        19
       
        20
       
        21
       
        22
       
        23
       
        24
       
        25
       
        26
       
        27
       
        28
       
        29
       
        30
       
        31
       
        32
       
        33
       
        34
       
        35
       
        36
       
        37
       
        38
       
        39
       
        40
       
        41
       
        42
       
        43
       
        44
       
        45
       
        46
       
        47
       
        48
       
        49
       
        50
       
        51
       
        52
       
        53
       
        54
       
        55
       
        56
       
        57
       
        58
       
        59
       
        60
       
        61
       
        62
       
        64
       
        65
       
        66
       
        67
       
        70
       
        71
       
        72
       
        73
       
        74
       
        75
       
        76
       
        77
       
        78
       
        79
       
        80
       
        81
       
        82
       
        83
       
        84
       
        85
       
        86
       
        87
       
        88
       
        89
       
        90
       
        91
       
        92
       
        93
       
        94
       
        95
       
        96
       
        97
       
        98
       
        99
       
        100
       
        102
       
        103
       
       
       

       
/*
       

       
       

        * Copyright (C) 2012 The Android Open Source Project
       

       
       

        *
       

       
       

        * Licensed under the Apache License, Version 2.0 (the "License");
       

       
       

        * you may not use this file except in compliance with the License.
       

       
       

        * You may obtain a copy of the License at
       

       
       

        *
       

       
       

        *      http://www.apache.org/licenses/LICENSE-2.0
       

       
       

        *
       

       
       

        * Unless required by applicable law or agreed to in writing, software
       

       
       

        * distributed under the License is distributed on an "AS IS" BASIS,
       

       
       

        * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
       

       
       

        * See the License for the specific language governing permissions and
       

       
       

        * limitations under the License.
       

       
       

        */

       

       
       

       
package com.example.android.immersivemode;
       

       
       

       

       
       

       
import android.os.Build;
       

       
       

       
import android.os.Bundle;
       

       
       

       
import android.support.v4.app.Fragment;
       

       
       

       
import android.view.MenuItem;
       

       
       

       
import android.view.View;
       

       
       

       

       
       

       
import com.example.android.common.logger.Log;
       

       
       

       

       
       

       
public class ImmersiveModeFragment extends Fragment {
       

       
       

       

       
       

       
public static final String TAG = "ImmersiveModeFragment";
       

       
       

       

       
       

       
@Override
       

       
       

       
public void onCreate(Bundle savedInstanceState) {
       

       
       

       
super.onCreate(savedInstanceState);
       

       
       

        setHasOptionsMenu
(true);
       

       
       

       
}
       

       
       

       

       
       

       
@Override
       

       
       

       
public void onActivityCreated(Bundle savedInstanceState) {
       

       
       

       
super.onActivityCreated(savedInstanceState);
       

       
       

       
final View decorView = getActivity().getWindow().getDecorView();
       

       
       

        decorView
.setOnSystemUiVisibilityChangeListener(
       

       
       

       
new View.OnSystemUiVisibilityChangeListener() {
       

       
       

       
@Override
       

       
       

       
public void onSystemUiVisibilityChange(int i) {
       

       
       

       
int height = decorView.getHeight();
       

       
       

       
Log.i(TAG, "Current height: " + height);
       

       
       

       
}
       

       
       

       
});
       

       
       

       
}
       

       
       

       

       
       

       
@Override
       

       
       

       
public boolean onOptionsItemSelected(MenuItem item) {
       

       
       

       
if (item.getItemId() == R.id.sample_action) {
       

       
       

        toggleHideyBar
();
       

       
       

       
}
       

       
       

       
return true;
       

       
       

       
}
       

       
       

       

       
       

       
/**
       

       
       

        * Detects and toggles immersive mode (also known as "hidey bar" mode).
       

       
       

        */

       

       
       

       
public void toggleHideyBar() {
       

       
       

       

       
       

       
// The UI options currently enabled are represented by a bitfield.
       

       
       

       
// getSystemUiVisibility() gives us that bitfield.
       

       
       

       
int uiOptions = getActivity().getWindow().getDecorView().getSystemUiVisibility();
       

       
       

       
int newUiOptions = uiOptions;
       

       
       

       
boolean isImmersiveModeEnabled =
       

       
       

       
((uiOptions | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY) == uiOptions);
       

       
       

       
if (isImmersiveModeEnabled) {
       

       
       

       
Log.i(TAG, "Turning immersive mode mode off. ");
       

       
       

       
} else {
       

       
       

       
Log.i(TAG, "Turning immersive mode mode on.");
       

       
       

       
}
       

       
       

       

       
       

       
// Navigation bar hiding:  Backwards compatible to ICS.
       

       
       

       
if (Build.VERSION.SDK_INT >= 14) {
       

       
       

        newUiOptions
^= View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
       

       
       

       
}
       

       
       

       

       
       

       
// Status bar hiding: Backwards compatible to Jellybean
       

       
       

       
if (Build.VERSION.SDK_INT >= 16) {
       

       
       

        newUiOptions
^= View.SYSTEM_UI_FLAG_FULLSCREEN;
       

       
       

       
}
       

       
       

       

       
       

       
// Immersive mode: Backward compatible to KitKat.
       

       
       

       
// Note that this flag doesn't do anything by itself, it only augments the behavior
       

       
       

       
// of HIDE_NAVIGATION and FLAG_FULLSCREEN.  For the purposes of this sample
       

       
       

       
// all three flags are being toggled together.
       

       
       

       
// Note that there are two immersive mode UI flags, one of which is referred to as "sticky".
       

       
       

       
// Sticky immersive mode differs in that it makes the navigation and status bars
       

       
       

       
// semi-transparent, and the UI flag does not get cleared when the user interacts with
       

       
       

       
// the screen.
       

       
       

       
if (Build.VERSION.SDK_INT >= 18) {
       

       
       

        newUiOptions
^= View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
       

       
       

       
}
       

       
       

       

       
       

        getActivity
().getWindow().getDecorView().setSystemUiVisibility(newUiOptions);
       

       
       

       
}
       

       
       

       
}