hide back, home and recents apps button in libgdx - java

Libgdx shows these buttons by default. Is it possible to hide them and show them only when user slides with finger down(I think this is how unity has done it)?

Instead of overriding android methods you can enable option in AndroidApplicationConfiguration
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
config.useImmersiveMode = true; // libgdx will handle it
initialize(new Main(), config);
}

To hide virtual Android buttons (on the phones that do not have physical buttons :) ) you have to set the application to use the Immersive Full-Screen mode.
You can achieve this by setting proper flags from android.view.View method by adding this code in your AndroidLauncher class
#TargetApi(19)
#Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if (hasFocus) {
getWindow().getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN |
View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION |
View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN |
View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
}
}
Take a look at this tutorial to get more information.

Related

How to have full-screen despite the notch?

I want an activity to be fullscreen despite the notch so that at its sides the screen is useful. Please does anyone know how to do it?
I guess it must be a different theme or something like that.
There is a fix for this given by Android Pie Apis.
Follow the following steps:-
1) Create a new values-v28 folder in your res directory and copy the default styles.xml file into it.
Create new or edit existing theme
<style name="ActivityTheme">
<item name="android:windowLayoutInDisplayCutoutMode">
shortEdges
</item>
</style>
There are 3 possible values for android:windowLayoutInDisplayCutoutMode ie - default,shortEdges and never . Try each to know the difference between them .
Doing this programmaticaly inside a activity which is what worked for me instead of defining a style will be as follows:
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
getWindow().setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
getWindow().getAttributes().layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
}
This code is what changes the whole scenario for devices with notch display
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
getWindow().setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
getWindow().getAttributes().layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
}
Also, if by any chance you still see the system ui the use the following code along with the above code
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE
| View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
Try adding this in your activity
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
This makes the activity full screen by removing navigation and status bars

Android full screen on only one activity?

My app has 3 activities(activity_main,activity_one,activity_two).
I want to full screen only the first activity so I searched and I found out I got to use this:
View decorView = getWindow().getDecorView();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
decorView.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);
}
When I go to the other activities that are not full screen and press the back button and go back to main activity the home button back button is there hiding the bottom of my main activity.How do I solve this?
Also if possible I would like to know how can I make my activity full screen but keep the action bar.
edit:
Im asking for a full screen on one activity and not the other two(except if the action bar stays).If i go on activity_two (which is not full screen) i have the home button back button etc .If i press the back button and go back to the main activity the buttons stay there which i don't want to (not only because i want my main full screen but because the buttons from activity two stay and hide the bottom of my main activity)
As onWindowsFocusChanged() is what you need so try this:1. First, Declare this variable as global in your MainActivity
private int currentApiVersion;
2. then paste this code in onCreate() of that activity.
currentApiVersion = Build.VERSION.SDK_INT;
final int flags = View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
if (currentApiVersion >= Build.VERSION_CODES.KITKAT) {
getWindow().getDecorView().setSystemUiVisibility(flags);
final View decorView = getWindow().getDecorView();
decorView.setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener() {
#Override
public void onSystemUiVisibilityChange(int visibility) {
if ((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0) {
decorView.setSystemUiVisibility(flags);
}
}
});
}
3. Now paste this method in your MainActivity class.
#SuppressLint("NewApi")
#Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if (currentApiVersion >= Build.VERSION_CODES.KITKAT && hasFocus) {
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_FULLSCREEN
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
}
}
Now this should work, give it a try.
You can do this in two ways,
Programmatically -- Set FullScreen flag to window of activity inside onCreate() method (just after or before setContentView()). Whom you want to be fullscreen. For example,
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
Window w = getWindow();
w.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
Or you can set theme in your AndroidManifest.xml as,
<activity
android:name=".YourActivityName"
android:theme="#android:style/Theme.NoTitleBar.Fullscreen"/>
Or If you are using AppCompatActivity then you can set as,
<activity
android:name=".YourActivityName"
android:theme="#android:style/Theme.AppCompat.Light.NoActionBar"/>
For more info about the window flags check this link https://developer.android.com/reference/android/view/WindowManager.LayoutParams.html

System Bars Flash then Disappear on Back in Immersive Sticky mode

I've read up on a lot of questions about immersive sticky mode on Android, but I cannot find a specific answer for my question.
I have read onBackPressed function not working under IMMERSIVE STICKY mode, but my app goes back successfully when an ImageButton is pressed.
I want to remain in immersive sticky mode for the whole duration of the app. There is no problem when I launch a new activity, with and without a shared element transition. However, when I go back, the system bars sometimes appears then soon slide out of view again. This happens especially when I stay on an activity for a while.
This transition is called from _TrickPage.class:
public void setBorroRingsOnClickListener(View view){
dBHelper.close();
Intent intent = new Intent(this, _DominoScene.class);
startActivity(intent);
}
And I call finish() to get back to _TrickPage.class from _Domino.class:
findViewById(R.id.picture).setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
dBHelper.close();
finish();
}
});
Another transition I use is a shared element transition. The following is an inner class of ButtonAdapter, which extends BaseAdapter, an object in my _Submenu Activity:
class MyOnClickListener implements View.OnClickListener {
private final int position;
public MyOnClickListener(int position){
this.position = position;
}
#Override
public void onClick(View v) {
...
//link clicked button to _TrickPage button for shared transition
v.setTransitionName("trick");
//make and start shared transition to _TrickPage
ActivityOptions options = ActivityOptions.makeSceneTransitionAnimation(
(Activity)mContext,
new Pair<View, String>(v, "trick")
);
Intent intent = new Intent(v.getContext(), _TrickPage.class);
v.getContext().startActivity(intent, options.toBundle());
}
}
Finally, to get back to _Submenu from _TrickPage, I call finishaftertransition():
picture.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dBHelper.close();
hideMainContent();
finishAfterTransition();
}
});
I set the UI flags in the onCreate() and onResume() methods of the Activities involved: _Submenu, _TrickPage, and _DominoScene. I also have the following in each activity:
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
//if (hasFocus)
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);
if (hasFocus && playAnimations) {
//showMainContent();
playAnimations = false;
}
}
This is my first time asking a question on Stack Overflow, so please ask for any other parts of my code if you think there could be a problem there. Thanks!
Call this method on your onCreate method from every Activity(right after your setContentView method). And also on the onWindowsFocusChanged
public void goImmersive()
{
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);
}

How to hide the preview of the app when it's in the task manager

I make a game with Java and LibGdx
It's a game of skill with a time limit, and the problem is the player can push home button to pause app, and go to task manager, and can see preview of app to seek what he has to find without counting time
So, I would like to hide the preview of the app when the player push home button to pause
What I tried to do
I override pause and resume handler
#Override
public void pause() {
Gdx.app.log("LibGDX", "pause");
state = State.PAUSE;
Gdx.graphics.setContinuousRendering(false);
Gdx.graphics.requestRendering();
}
#Override
public void resume() {
Gdx.app.log("LibGDX", "resume");
state = State.RUN;
Gdx.graphics.setContinuousRendering(true);
}
and main part of my public void render(float) function
switch (state)
{
case RUN:
// game
break;
case PAUSE:
// a black rectangle on screen
shapeRenderer.setProjectionMatrix(camera.combined);
shapeRenderer.begin(ShapeRenderer.ShapeType.Filled);
shapeRenderer.setColor(Color.BLACK);
shapeRenderer.rect(0, 0, GameScreen.WIDTH, GameScreen.HEIGHT);
shapeRenderer.end();
break;
default:
break;
}
log tell me that pause and resume are indeed invoked
but in task manager, I still see the game and not the black rectangle
Update 1
to Ridcully answer :
I tried what you suggested
package com.mygdx.game;
import android.os.Bundle;
import android.view.WindowManager;
import com.badlogic.gdx.backends.android.AndroidApplication;
import com.badlogic.gdx.backends.android.AndroidApplicationConfiguration;
public class AndroidLauncher extends AndroidApplication {
#Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
// --
getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);
// --
initialize(new MyGame(), config);
}
}
But same thing, I still see the screenshot
I'm with Android Studio 2.1
My smartphone: Oneplus One
and Android 6.0.1
Do you have an idea ? thanks
There is a flag for that. You can use this little method, e.g. in onCreate() to prevent Android from creating a 'screenshot' for the 'recent activities' list. You can activate/deactivate as you want, e.g. only activate it when the actual game board is visible etc.
/**
* Sets/clears FLAG_SECURE which prevents system from creating screenshots for 'recent activities' list.
* Flag should be set while user is in vault.
*
* #param set
*/
protected void setWindowFlagSecure(boolean set) {
if (set) {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);
} else {
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_SECURE);
}
EDIT
I just found this SO answer, regarding Libgdx applications: Seems you have to set the flag AFTER invoking the initialize() method.
You can do that using
public class SampleActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE,
WindowManager.LayoutParams.FLAG_SECURE);
setContentView(R.layout.main);
}
}
Make sure you do that before you call setContentView.

Force android libgdx into immersive mode

Following the tutorials on this page(written in 2009) don't work any more. http://www.androidsnippets.com/how-to-make-an-activity-fullscreen
public class FullScreen extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main);
}
}
Similarily, trying to change the theme in android.xml has no effect:android:theme="#android:style/Theme.NoTitleBar.Fullscreen"
Here is a screenshot:
After scouring the web for a few hours, i cant find any articles that can help me.
So how, in version 1.5.4 of libgdx, can i get an immersive full screen?
----EDIT----
Here is the now fully working code:
public class AndroidLauncher extends AndroidApplication {
#Override
protected void onCreate (Bundle savedInstanceState) {
hideSystemUi();
super.onCreate(savedInstanceState);
AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
config.hideStatusBar=true;
config.useImmersiveMode=true;
initialize(new game(), config);
}
private void hideSystemUi() {
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);
}
}
Try this in the android activity:
#Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if (hasFocus) {
getWindow().getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN |
View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION |
View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN |
View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
}
}
Note this will only work on API level 19 or above.
Sadly I can't comment due to my current reputation,
however I have found that adding:
config.hideStatusBar = true;
config.useImmersiveMode = true;
in the AndroidLauncher class is enough to show the app in immersive however the app will start in standard mode and then update to immersive mode, more info on the Android immersive mode:
When you use the SYSTEM_UI_FLAG_IMMERSIVE_STICKY flag, an inward swipe in the system bars areas causes the bars to temporarily appear in a semi-transparent state, but no flags are cleared, and your system UI visibility change listeners are not triggered. The bars automatically hide again after a short delay, or if the user interacts with the middle of the screen.
Android sdk page
So my conclusion would be that config.useImmersiveMode use the sticky flag.
On a side note,
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
has to be added to the OnWindowFocusChanged method when targeting an api lvl less than 11

Categories