Listen for first TouchEvent when using SYSTEM_UI_FLAG_HIDE_NAVIGATION

Starting with API 14 you can use this flag SYSTEM_UI_FLAG_HIDE_NAVIGATION on a View within your Activity to tell the system you want to hide the navigation bar until there is user interaction (screen touch). Once the user taps the screen the bar is shown.

The Activity that I am doing this in takes some action (starts a separate activity) when the user touches the screen, before adding the above flag to my view this worked perfectly.

  • the differences between createRfcommSocketToServiceRecord and createRfcommSocket
  • How to properly highlight selected item on RecyclerView?
  • Clarify the Firebase connections
  • Event when sim card is changed
  • Building an Android app with targetSdkVersion of 25 and minSdkVersion 19
  • Android: Resize only parts of view with soft keyboard on screen
  • After adding that flag the system intercepts the first screen touch and reacts to it by showing the nav bar. It’s not until the second touch that any of my Views, or my Activity are receiving a TouchEvents.

    Does anyone know of a way that I can set up a listener that will let me launch my second activity the first time the screen is touched instead of needing to double tap when using this hide nav flag?

    I’ve tried all of the following and I’m not getting callbacks to any of them when the screen is touched for the first time to show the nav bar.

    @Override
    public void onUserInteraction(){
        Log.i(myTag, "INTERACT");
    }
    
    @Override
    public boolean onGenericMotionEvent(MotionEvent me){
        Log.i(myTag, "GENERIC");
    
        return true;
    }
    
    //I thought maybe the size change would lead to a callback here. No dice though.
    @Override 
    public void onWindowAttributesChanged(WindowManager.LayoutParams params){
        Log.i(myTag, "WINDOW CHANGE");
    }
    @Override
    public boolean dispatchTouchEvent(MotionEvent me){
        Log.i(myTag, "TOUCH");
        return true;
    }
    

    Note: I am not trying to prevent the nav bar from being shown upon the first touch, I just want to also take some other action when that event occurs.

  • Create a custom Drawable and use it in XML
  • Refused to load the script because it violates the following Content Security Policy directive
  • Scroll to top in RecyclerView with LinearLayoutManager
  • Import Project from IntelliJ IDEA to Android Studio
  • Android - file provider - permission denial
  • How to use Dagger's 2 @Named qualifier in Kotlin
  • One Solution collect form web for “Listen for first TouchEvent when using SYSTEM_UI_FLAG_HIDE_NAVIGATION”

    As Josh Lee suggested in his comment, View.OnSystemUiVisibilityChangeListener was the key.

    Here is the code that I used:

    mView.setOnSystemUiVisibilityChangeListener(new OnSystemUiVisibilityChangeListener() {
        @Override
        public void onSystemUiVisibilityChange(int vis) {
            Log.i(myTag, "System UI"+ vis);
            if(vis == 0){
                Intent i = new Intent(MainActivity.this, AnotherActivity.class);
                startActivity(i);
                finish();
            }
        }
    });
    

    I think that mView could be a reference to any view that is currently showing in your Activity. In my case it was a fullscreen VideoView, and was the only view in my layout.

    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.