Hide bottom nav bar - (Android Essentials: XML & Java)
import android.os.Bundle;
import android.view.View;
import android.view.Window;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Hide the bottom navigation bar
hideBottomNavigationBar();
setContentView(R.layout.activity_main);
}
//-----------------
private void hideBottomNavigationBar() {
Window window = getWindow();
window.getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_IMMERSIVE);
}
// ... rest of your code
}
Enhance the full-screen experience of your Android application by hiding the bottom navigation bar. This is especially useful when you want your app to utilize the entire screen space for content display, such as in games, video players, or reading apps. The provided code snippet is designed to be placed in your MainActivity
or any other activity where you wish to hide the system's bottom navigation bar.
Here's what the code does:
When the activity is created, it invokes the
hideBottomNavigationBar
method before setting the content view.The
hideBottomNavigationBar
method retrieves the current window and applies system UI visibility flags.The flags used,
SYSTEM_UI_FLAG_HIDE_NAVIGATION
andSYSTEM_UI_FLAG_IMMERSIVE
, combine to hide the bottom navigation bar and provide an immersive, sticky interaction where the navigation bar will not show unless swiped from the edge of the screen.
How to use this code:
Ensure you import the required classes by copying the import statements into your activity file.
Copy the
MainActivity
class into your Android project. If you already have a main activity, integrate thehideBottomNavigationBar
method and the call to this method within youronCreate
method.Place the
setContentView
call after thehideBottomNavigationBar
call to ensure the navigation bar is hidden before the layout is displayed.
Note:
The system UI visibility flags are deprecated as of API level 30 in favor of new WindowInsets and WindowInsetsController APIs. If you are targeting Android 11 (API level 30) or above, consider using the recommended APIs for compatibility with newer devices.
Keep in mind that hiding the system navigation bar can affect how users navigate your app and the device. Make sure to implement intuitive gestures or provide clear instructions on how to navigate within your app when the navigation bar is hidden.