Android Tutorial Immersive Mode - hide the navigation bar

Discussion in 'Tutorials & Examples' started by Erel, Mar 21, 2018.

Thread Status:
Not open for further replies.
  1. Erel

    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
     

    Attached Files:

    Last edited: Nov 5, 2018
  2. Foz

    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 :)
     
  3. Erel

    Erel Administrator Staff Member Licensed User

    Good point. I've uploaded a new version that fixes this.
     
    eps and Foz like this.
  4. Foz

    Foz Member Licensed User

    And indeed it does - thanks Erel!
     
  5. Erel

    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 and Johan Hormaza like this.
  6. coslad

    coslad Well-Known Member Licensed User

    Hi

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

    Code:
    Sub Activity_WindowFocusChanged(HasFocus As Boolean)
    log :

    Code:
    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: Jun 15, 2018
  7. coslad

    coslad Well-Known Member Licensed User

  8. Erel

    Erel Administrator Staff Member Licensed User

    If it only happens in debug mode then ignore it. This is a problematic event as it fires in "unexpected" points.

    What is not complete?
     
  9. coslad

    coslad Well-Known Member Licensed User

    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 :

    Code:
    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
     
  10. Erel

    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.
     
  11. Alessandro71

    Alessandro71 Member Licensed User

    what's that Sleep(300) in Activity_WindowFocusChanged for?
     
  12. Erel

    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.
     
    Alessandro71 likes this.
  13. FrankDev

    FrankDev Active Member Licensed User

    Last edited: Nov 2, 2018
  14. Erel

    Erel Administrator Staff Member Licensed User

    Please start a new thread for this question.
     
Thread Status:
Not open for further replies.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice