Reputation: 311
How can I detect the presence of the navigation bar and hide it?
In my onCreate()
I call hideNavigationBar()
method to hide the navigation bar, then I register a listener to hide the navigation bar every time it becomes visible when the user touches anywhere on the screen as reported by the documentations. When the navigation bar becomes visible after a touch event the hideNavigationBar()
method is called again by the listener, but it has not effect, the bar is still visible.
This is my onCreated()
method:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
hideNavigationBar();
View decorView = getWindow().getDecorView();
decorView.setOnSystemUiVisibilityChangeListener
(new View.OnSystemUiVisibilityChangeListener() {
@Override
public void onSystemUiVisibilityChange(int visibility) {
if ((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0) {
Toast.makeText(getApplicationContext(), "Visible", Toast.LENGTH_SHORT).show();
hideNavigationBar();
} else {
Toast.makeText(getApplicationContext(), "Not visible", Toast.LENGTH_SHORT).show();
}
}
});
}
and this is my hideNavigationBar()
method:
private void hideNavigationBar() {
Toast.makeText(getApplicationContext(), "hideNavigationBar()", Toast.LENGTH_SHORT).show();
View decorView = getWindow().getDecorView();
int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_FULLSCREEN;
decorView.setSystemUiVisibility(uiOptions);
}
How can I hide the navigation bar every time it becomes visible?
Thanks
Upvotes: 10
Views: 19306
Reputation: 639
you could add this code to your activity's onCreate() method:
View decorView = getWindow().getDecorView();
decorView.setOnSystemUiVisibilityChangeListener
(new View.OnSystemUiVisibilityChangeListener() {
@Override
public void onSystemUiVisibilityChange(int visibility) {
if ((visibility & View.SYSTEM_UI_FLAG_HIDE_NAVIGATION) == 0) {
// TODO: The navigation bar is visible. Make any desired
// adjustments to your UI, such as showing the action bar or
// other navigational controls.
hideNavigationBar()
} else {
// TODO: The navigation bar is NOT visible. Make any desired
// adjustments to your UI, such as hiding the action bar or
// other navigational controls.
}
}
});
It's generally good practice to keep your UI in sync with changes in system bar visibility. For example, you could use this listener to hide and show the action bar in concert with the status bar hiding and showing.Android-Responding to UI Visibility Changes
Upvotes: 13
Reputation:
Try calling hideNavigationBar()
in a Runnable
using View.post(Runnable)
. For example:
...
if ((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0) {
decorView.post(new Runnable() {
@Override
public void run() {
hideNavigationBar();
}
});
}
...
Upvotes: 0
Reputation: 1869
Do this:
boolean hasMenuKey = ViewConfiguration.get(context).hasPermanentMenuKey();
boolean hasBackKey = KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_BACK);
if(!hasMenuKey && !hasBackKey) {
// Do whatever you need to do, this device has a navigation bar
}
Original answer Check for navigation bar
Upvotes: 3