Bottom Navigation in Android Development
Introduction
Bottom Navigation is a UI component in Android that allows users to quickly navigate between top-level views in an application. It is typically used when the app has three to five top-level destinations.
Setting Up Bottom Navigation
To start using Bottom Navigation, you need to add the necessary dependencies to your project. Open your build.gradle file and add the following dependency:
dependencies {
implementation 'com.google.android.material:material:1.3.0'
}
Creating the Bottom Navigation Menu
Create a menu resource file in the res/menu directory. Name the file bottom_nav_menu.xml and add the following content:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/nav_home"
android:icon="@drawable/ic_home"
android:title="Home" />
<item
android:id="@+id/nav_search"
android:icon="@drawable/ic_search"
android:title="Search" />
<item
android:id="@+id/nav_notifications"
android:icon="@drawable/ic_notifications"
android:title="Notifications" />
<item
android:id="@+id/nav_profile"
android:icon="@drawable/ic_profile"
android:title="Profile" />
</menu>
Adding Bottom Navigation to Your Layout
Next, add the Bottom Navigation component to your activity's layout file (e.g., activity_main.xml):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<fragment
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/bottom_navigation_view"
app:defaultNavHost="true"
app:navGraph="@navigation/nav_graph" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_navigation_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="?android:attr/windowBackground"
app:menu="@menu/bottom_nav_menu" />
</RelativeLayout>
Handling Navigation Item Selections
In your activity (e.g., MainActivity.java), set up an OnNavigationItemSelectedListener for the Bottom Navigation view:
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import com.google.android.material.bottomnavigation.BottomNavigationView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BottomNavigationView bottomNavigationView = findViewById(R.id.bottom_navigation_view);
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.nav_home:
// Handle home navigation
return true;
case R.id.nav_search:
// Handle search navigation
return true;
case R.id.nav_notifications:
// Handle notifications navigation
return true;
case R.id.nav_profile:
// Handle profile navigation
return true;
}
return false;
}
});
}
}
Conclusion
Bottom Navigation is a powerful tool for organizing top-level views in your Android app. By following this tutorial, you should now have a functional Bottom Navigation component integrated into your application. For further customization, refer to the official Android documentation.
