Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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.

like image 255
FoamyGuy Avatar asked Aug 01 '12 15:08

FoamyGuy


1 Answers

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.

like image 190
FoamyGuy Avatar answered Nov 15 '22 00:11

FoamyGuy