Android Tutorial Immersive Mode - hide the navigation bar

Status
Not open for further replies.

Erel

Administrator
Staff member
Licensed User
Immersive mode means full screen mode where the navigation bar is also hidden.

The user can bring back the bars by swiping near the edges.

The required steps are:

1. Call setSystemUiVisibility with the immersive flags. This is done in Activity_WindowFocusChanged.
2. Get the full display size and set the activity to that size.
3. Load the layout

Depends on JavaObject and Phone libraries.

Example is attached.

Edit: example of immersive mode that keeps the status bar visible: https://www.b4x.com/android/forum/threads/immersive-mode-hide-only-navigation-bar.98988/#post-623345
 

Attachments

Last edited:

Foz

Member
Licensed User
Thanks for this Erel, however there is a slight issue that 100%y is still fixed to the original size.
Obviously replacing 100%y with lv.Height resolves it (I have to store it, Activity.Height doesn't seem to adjust), it was just a gotcha for me :)
 

Erel

Administrator
Staff member
Licensed User
The code was updated with a small change.

The Log(LastException) line was commented. The Activity_WindowFocusChanged event can fire after the activity was paused and this line can cause an untrapped exception to be raised.
 

coslad

Well-Known Member
Licensed User
Hi

randomly when i change activity the app crash , at line 162 i have

B4X:
Sub Activity_WindowFocusChanged(HasFocus As Boolean)
log :

B4X:
Error occurred on line: 162 (Main)
java.lang.NullPointerException: Attempt to read from field 'anywheresoftware.b4a.BA b4a.casa.tablet.main.activityBA' on a null object reference
    at b4a.casa.tablet.main._activity_windowfocuschanged(main.java:629)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:735)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:357)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:260)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
    at b4a.casa.tablet.main.onWindowFocusChanged(main.java:185)
    at com.android.internal.policy.DecorView.onWindowFocusChanged(DecorView.java:1451)
    at android.view.View.dispatchWindowFocusChanged(View.java:10258)
    at android.view.ViewGroup.dispatchWindowFocusChanged(ViewGroup.java:1193)
    at android.view.ViewRootImpl$ViewRootHandler.handleMessage(ViewRootImpl.java:3605)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6119)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

PS the errors seems happen only in debug mode
 
Last edited:

coslad

Well-Known Member
Licensed User
"What is not complete?"
I try to explain , when the app is into immersive mode :

immersive_mode.jpg

if the user touch ed edge , the system bar reappears :

non_immersive_mode.jpg


I need to intercept the change to let my code resize the layout , to do this i find from Google development site :

To get notified of system UI visibility changes, register an View.OnSystemUiVisibilityChangeListener to your view. This is typically the view you are using to control the navigation visibility.
It's generally good practice to keep your UI in sync with changes in system bar visibility. For example, you could use this listener to hide and show the action bar in concert with the status bar hiding and showing.
B4X:
View decorView = getWindow().getDecorView();
decorView.setOnSystemUiVisibilityChangeListener
        (new View.OnSystemUiVisibilityChangeListener() {
    @Override
    public void onSystemUiVisibilityChange(int visibility) {
        // Note that system bars will only be "visible" if none of the
        // LOW_PROFILE, HIDE_NAVIGATION, or FULLSCREEN flags are set.
        if ((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0) {
            // TODO: The system bars are visible. Make any desired
            // adjustments to your UI, such as showing the action bar or
            // other navigational controls.
        } else {
            // TODO: The system bars are NOT visible. Make any desired
            // adjustments to your UI, such as hiding the action bar or
            // other navigational controls.
        }
    }
});

but i can't make it works into B4A .

Any idea ?

Thanks
 

Erel

Administrator
Staff member
Licensed User
In most cases I don't think that you should do anything when the bars appear as they only appear for a short period. It should be possible to add this listener with JavaObject. Start a new thread in the questions forum and I'll show you how.
 

Erel

Administrator
Staff member
Licensed User
WindowFocusChanged event fires during transitions. The purpose of the sleep call is to make sure that the transition has ended.
 
Status
Not open for further replies.
Top