codingleisurely-blog
codingleisurely-blog
Born to make mistakes, not to fake perfection
1 post
Don't wanna be here? Send us removal request.
codingleisurely-blog · 10 years ago
Text
How to Show/hide Navigation Buttons in an Android App
Have you ever used any app where 3 buttons(back, home, recent) are not visible
Well, I was trying to make an app recently where I needed these buttons on some screens and wanted to hide them under some scenarios. So basically the structure of my app was as following:
There was an Activity. Let’s call it MainActivity.java
Then there were two fragments. Lets call them showFragment.java and hideFragment.java
I Wanted to show a ViewPager inside my activity where people could just swipe left to right to see different fragments. In ShowFragment I wanted to show the navigation buttons whereas in HideFragment i wanted to hide them.
Below are the code snippets which I used to perform the above mentioned task:
MainActivity.java
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); } private void hideNavigationBars() { getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); if (Build.VERSION.SDK_INT < 19) { View v = this.getWindow().getDecorView(); v.setSystemUiVisibility(View.GONE); } else { View decorView = getWindow().getDecorView(); int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY | View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION; decorView.setSystemUiVisibility(uiOptions); } } @Override protected void onResume() { hideNavigationBars(); super.onResume(); }
ShowFragment.java
I created a method showNavigationBars(). Call this from OnResume() method.
private void showNavigationBars() { getActivity().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); if (Build.VERSION.SDK_INT < 19) { View v = getActivity().getWindow().getDecorView(); v.setSystemUiVisibility(View.GONE); } else { View decorView = getActivity().getWindow().getDecorView(); int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION; decorView.setSystemUiVisibility(uiOptions); } }
HideFragment.java
In this I used a method hideNavigationBars(). Call this from OnResume() method.
private void hideNavigationBars() { getActivity().getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); if (Build.VERSION.SDK_INT < 19) { View v = getActivity().getWindow().getDecorView(); v.setSystemUiVisibility(View.GONE); } else { View decorView = getActivity().getWindow().getDecorView(); int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY | View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION; decorView.setSystemUiVisibility(uiOptions); } }
The above code works well for me but during the research I found out few other important flags
getWindow().getDecorView().setSystemUiVisibility( View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_FULLSCREEN)
This will show the navigation bar only on screen touch, but recreates the view so that view does not hide behind the navigation bar
getWindow().getDecorView(); int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY; decorView.setSystemUiVisibility(uiOptions);
This will hide the navigation bar
getActivity().getWindow().getDecorView().setSystemUiVisibility( View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
This will show the navigation bar only on screen touch, view hides behind the navigation bar
Please let me know if you face any problem with the code. Also I was unable to figure out one issue. i.e. if you hide the navigation bar using above code and your phone goes to lock mode then navigation bar will automatically appear.
0 notes