I seem to have an issue with hiding and showing the ActionBar. My app starts with it hidden and I want it to appear back when the users swipes down on any part of the screen and hide it back then the user swipes up.
I have tried implementing this in the following ways:
Method 1: in onCreate
View mView = getWindow().getDecorView();
//mView.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_IMMERSIVE;
mView.setSystemUiVisibility(uiOptions);
mView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
Toast.makeText(context, "Swiped down", Toast.LENGTH_SHORT).show();
//getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
getSupportActionBar().show();
return true;
case MotionEvent.ACTION_UP:
Toast.makeText(context, "Swiped up", Toast.LENGTH_SHORT).show();
//getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
getSupportActionBar().hide();
return true;
default: return false;
}
//return false;
}
});
Method 2:
@Override
public boolean onTouchEvent(MotionEvent event) {
int action = event.getActionMasked();
switch (action) {
case (MotionEvent.ACTION_DOWN):
Toast.makeText(context, "Swiped down", Toast.LENGTH_SHORT).show();
getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
getSupportActionBar().show();
return true;
case (MotionEvent.ACTION_UP):
Toast.makeText(context, "Swiped up", Toast.LENGTH_SHORT).show();
getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
getSupportActionBar().hide();
return true;
default:
return super.onTouchEvent(event);
}
}
Now here are the issues:
- The function applies only on parts of the screens where I have no buttons or other elements which can be clickable;
- Where it applies, it has nothing to do with swipes. It shows the bar as long as I keep the finger pressed on a non-clickable filed in the view.
EDIT:
With the replies from the comment, I managed to make the Action Bar show and hide based on scroll, but I still have the issue with #1, in which I cannot perform the action on any part of the screen.
It works only on the parts where onClickListener
is not assessed.