I'm trying to hide the top and bottom navigation bars on Android devices when I run my app. Right now, I have modified my "onWindowFocusChanged" and "onCreate" methods to have the following code fragment:
final int screen =
View.SYSTEM_UI_FLAG_HIDE_NAVIGATION |
View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN |
View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION |
View.SYSTEM_UI_FLAG_LAYOUT_STABLE |
View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY |
View.SYSTEM_UI_FLAG_FULLSCREEN;
getWindow().getDecorView().setSystemUiVisibility(screen);
However, when I go back to the home screen on Android without closing the app, reopen my app, and then try to switch between pages on my app, the navigation bar briefly appears and then disappears between these switches. I assume this is because the above methods are not being accessed.
How can I make sure that even when a user abruptly switches out of my app and then back into it, that the navigation bar will not briefly reappear when switching pages as a result?
Thanks for the help.
You can look at the guide here: https://developer.android.com/training/system-ui/navigation
You need to add the code to three places:
onWindowFocusChanged, onCreate and onResume
Related
I recently converted my app to Single Activity architecture and trying to set the one particular fragment to fullscreen. The standard fullscreen code for activity does not work for fragments
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_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
I found this Android set full screen from fragment. But settings it to FLAG_LAYOUT_NO_LIMITS causes the PagerSnapHelper in the fragment to jump around when switching between FLAG_LAYOUT_NO_LIMITS and revoking it.
So my question is, is there any other way to set fragment to fullscreen without using FLAG_LAYOUT_NO_LIMITS.
You shouldn't ever need to use FLAG_LAYOUT_NO_LIMITS, nor should you apply fitsSystemWindows="true" globally at your activity layer - that is what would prevent your fragment from going full screen. Instead, only add fitsSystemWindows on the fragments / individual components that would otherwise overlap the status/system bars. This would ensure that your full screen fragment would be able to take up the full size and not be inset from the edges.
I need to completely remove the actionbarr and navigationbar from my app. So I stay in fullscreen without the possibility to swipe so I get the navigationbar back.
I have now this code in my onreate method which makes it go away but as soon as you swipe the navigation bar will appear again. How can I avoid this?
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);
On your activity override onBackPressed() to prevent activity from going back when back button is pressed. By default it runs finish() method if there is no backStack to run through.
On your App theme for the activity, make sure it's a theme that has NoActionBar on it that way it won't have one unless you put one into layout.
style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"
I'm developing a one-view app which contains a fullscreen WebView. I started the project from a fullscreen template and added the code from both this and this official docs pages.
When the app is first started it works good and i obtain a fullscreen with no status bar. Problem is that when i exit the app (either opening the task manager or pressing the button that leads to the home) and then re-enter in it, the status bar is displayed and there's no way to hide it back if not closing and re-opening the app.
How can i fix this?
UPDATE
Here's the source code:
AndroidManifest.xml
FullscreenWebView.java
activity_fullscreen_web_view.xml
As already told, I haven't made much customization from the standard template of Android Studio.
The fullscreen code I've added is this, anyway:
View decorView = getWindow().getDecorView();
int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_FULLSCREEN;
decorView.setSystemUiVisibility(uiOptions);
setContentView(R.layout.activity_fullscreen_web_view);
so i'm writing an app in which i would like the navigation bar in a specific
activity to be hidden...and i managed to do so...but my problem is when the
user scrolls down the notification bar...when that happens...the navigation bar
re-appears...how can i solve this problem?
final int mUIFlag = View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_IMMERSIVE
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_FULLSCREEN;
//View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION;
getWindow().getDecorView().setSystemUiVisibility(mUIFlag);
when the user scrolls down the notification bar...when that happens...the navigation bar re-appears
I think this behavior is normal and intended as UI design of Android.
Whenever a user swipes-down upper edge or swipes-up bottom edge of screen, both system bar and navigation bar re-appear. Then after a few seconds, they are both disappear again. So as to the user can access system bar or navigation bar whenever they want even if screen is in immersive mode.
can i do anything to disable this? or maybe only hide it
No, generally (without root access) you can't. Only you can do is to hide it. Please refer to these StackOverflow Q&As: Permanently hide navigation bar on activity; Hide System Bar in Tablets.
I tried setting systemUIView(View.GONE) and use Immersive Full-Screen Mode. But users can always get the naviagtion bar back by touching the bottom of the screen. The apps i mentioned above are able to hide it without root or setting default launcher.
Okay, I found a solution finally and here is how it's done:
Use SYSTEM_UI_FLAG_IMMERSIVE_STICKY to hide the navigation bar as follow, you may put the code inside onResume of your activity
View decorView = getWindow().getDecorView();
int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION |View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
decorView.setSystemUiVisibility(uiOptions);
Then, add a system error window using WindowManger and overlay it on top of everything
you can put this unescapable view anywhere you like, but if you want to do it while users locked the screen, add this flag:
getWindow().setFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED, WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
Et voilĂ
Well I've never achieved this but it seems that you will need to set some other flags to get this kind of view :
When you use the SYSTEM_UI_FLAG_IMMERSIVE flag, it hides the system bars based on what other UI flags you have set (SYSTEM_UI_FLAG_HIDE_NAVIGATION, SYSTEM_UI_FLAG_FULLSCREEN, or both). When the user swipes inward in a system bars region, the system bars reappear and remain visible.
Here is a snippet that you can use to set these flags :
// This snippet hides the system bars.
private void hideSystemUI() {
// Set the IMMERSIVE flag.
// Set the content to appear under the system bars so that the content
// doesn't resize when the system bars hide and show.
mDecorView.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 // hide nav bar
| View.SYSTEM_UI_FLAG_FULLSCREEN // hide status bar
| View.SYSTEM_UI_FLAG_IMMERSIVE);
}
// This snippet shows the system bars. It does this by removing all the flags
// except for the ones that make the content appear under the system bars.
private void showSystemUI() {
mDecorView.setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
}
For more information go check Using Immersive Full-screen Mode from the android developer website.
Hope this help.