Bottom menu bar in android
To create a bottom menu bar in Android, you can use the BottomNavigationView class provided by the Android SDK. Here's the full code to create a basic bottom menu bar with three menu items:
First, in your app's layout XML file, add the BottomNavigationView widget:
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:menu="@menu/bottom_menu" />
Note that we're referencing a menu resource file called
bottom_menu
, which we'll create next.
Next, create a new XML file called bottom_menu.xml
in your
app's res/menu
directory. This file will define the menu items
for the bottom navigation bar:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/menu_home"
android:title="Home"
android:icon="@drawable/ic_home" />
<item
android:id="@+id/menu_search"
android:title="Search"
android:icon="@drawable/ic_search" />
<item
android:id="@+id/menu_profile"
android:title="Profile"
android:icon="@drawable/ic_profile" />
</menu>
In this example, we have three menu items: Home, Search, and Profile. Each item has an ID, a title, and an icon. You can replace the icons with your own drawable resources.
Finally, in your activity's Java code, you can set up the bottom navigation view:
BottomNavigationView bottomNavigationView = findViewById(R.id.bottom_navigation);
bottomNavigationView.setOnNavigationItemSelectedListener(
new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_home:
// Handle Home click
return true;
case R.id.menu_search:
// Handle Search click
return true;
case R.id.menu_profile:
// Handle Profile click
return true;
}
return false;
}
});
This code sets up a listener for when the user selects a menu item. In the listener, you can handle the click for each menu item and perform the appropriate action.
That's it! With these steps, you should now have a functional bottom menu bar in your Android app.