Bigosaur blog

How to properly handle fullscreen immersive move in Android KitKat

I noticed that on newer devices, the resolutions from my previous post are not always fully available. After some investigation, it turned out that a part of screen is used for Android interface buttons (back, home, menu) which are hardware buttons on some devices and virtual buttons on other.

To get the full screen and still have the resolution math from my previous post functional, we need to enter the immersive mode. This a feature available since KitKat (Android 4.4). I experimented with various devices and finally arrived to this code:

public class MainActivity extends AndroidApplication {

    private Handler mHandler = new Handler();
    private Runnable decor_view_settings;
    private int viewFlags = 0;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

        if (Build.VERSION.SDK_INT >= 19)    // KitKat
        {
            requestWindowFeature(Window.FEATURE_NO_TITLE);
            setupBars19();
        }

        AndroidApplicationConfiguration cfg = new AndroidApplicationConfiguration();
        cfg.useGL20 = false;

        initialize(new GosGame(), cfg);
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus)
    {
        if (Build.VERSION.SDK_INT < 19)	// KitKat
            return;

        if (hasFocus)
            mHandler.postDelayed(decor_view_settings, 5000);
        else
            mHandler.removeCallbacks(decor_view_settings);
    }

    @TargetApi(19)
    public void setupBars19()
    {
        viewFlags = View.SYSTEM_UI_FLAG_LAYOUT_STABLE
            | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
            | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
            | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
            | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
            | View.SYSTEM_UI_FLAG_FULLSCREEN;

        decor_view_settings = new Runnable()
        {
            public void run()
            {
                getWindow().getDecorView().setSystemUiVisibility(viewFlags);
            }
        };

        View decorView = getWindow().getDecorView();
        decorView.setSystemUiVisibility(viewFlags);

        decorView.setOnSystemUiVisibilityChangeListener
        (new View.OnSystemUiVisibilityChangeListener() {
            @Override
            public void onSystemUiVisibilityChange(int visibility) {
                if ((visibility & View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY) == 0) {
                    mHandler.postDelayed(decor_view_settings, 5000);
                }
            }
        });
    }
}

The code compiles on all versions, but only does stuff when the system is KitKat. As I wrote, I tested various devices (Nexus 7 and 10, Galaxy S3 and S4, etc.). It seems to work perfectly.

read more...   Bigosaur, 2014-10-04


<< Supporting multiple resolutions in 2D games


ImageMagic scripts to scale graphics for different Android screens >>

Son of a Witch | SoaW graphics, screenshots and .gifs
Bigosaur.com Website Home Page Bigosaur.com Website Home Page Blog main page YouTube channel Twitter account Google+ page Facebook page