Skip to content

Instantly share code, notes, and snippets.

@leoncvlt
Last active November 16, 2019 15:11
Show Gist options
  • Save leoncvlt/f398e629ec1ca5bad9b0 to your computer and use it in GitHub Desktop.
Save leoncvlt/f398e629ec1ca5bad9b0 to your computer and use it in GitHub Desktop.
Immersive Sticky Mode Android Haxeflixel

Using Immersive Sticky Mode (Android 4.4+)

The Immersive Mode on Android hides all system bars when the app is running, thus giving you access to the full width and height of the screen (this is useful for getting integer aspect ration when zooming).First you have to find the the template GameActivity.java file that the system uses to generate the apps code. Usually it is located at HaxeToolkit\haxe\lib\lime[currentVersion]\templates\android\template\src\org\haxe\lime then modify it to include

import android.view.KeyEvent; 

in the imports field, and

private void setSystemUiVisibility() {
  ::if WIN_FULLSCREEN::
  ::if (ANDROID_TARGET_SDK_VERSION >= 19)::
  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
    getWindow().getDecorView().setSystemUiVisibility(
    View.SYSTEM_UI_FLAG_LAYOUT_STABLE |
    View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION |
    View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN |
    View.SYSTEM_UI_FLAG_HIDE_NAVIGATION |
    View.SYSTEM_UI_FLAG_FULLSCREEN |
    View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
    );
  }
  ::elseif (ANDROID_TARGET_SDK_VERSION >= 16)::
  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
    getWindow().getDecorView().setSystemUiVisibility(
    View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN |
    View.SYSTEM_UI_FLAG_LAYOUT_STABLE |
    View.SYSTEM_UI_FLAG_LOW_PROFILE |
    View.SYSTEM_UI_FLAG_FULLSCREEN
    );
  }
  ::end::
  ::end::
}

private final Runnable activateImmersiveMode = new Runnable() {
@Override
public void run() {
  setSystemUiVisibility();
  }
};

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN || keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
  mHandler.postDelayed(activateImmersiveMode, 500);
}
  return super.onKeyDown(keyCode, event);
}

@Override protected void onResume() {
  super.onResume();
  setSystemUiVisibility();
  // rest of onResume()
}

@Override protected void onStart() {
  super.onStart();
  setSystemUiVisibility();
  // rest of onStart()
}

in the code's body. Note that some methods may already be present in the file (like onStart() ), so insted of copy-pasting the whole code it's better if you skim through the file and implement the new code in the ones which are already present.

After this, edit your project.xml file to include

<android target-sdk-version="19" if="android" />

The immersive mode was implemented from android 4.4 forward, so we want the project to target that android version where possible. Your game will still work in previous versions, but it won't activate the immersive mode.

See this Stack Overflow Thread for reference. Thanks to @goshi for pointing me in the right direction.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment