How to create bottom navigation with help of navigation resource and menu in android studio.
I was trying to create bottom navigations but I click another bottom navigations option its not working.
please help me and suggest me what I done wrong
MainActivity.java:
import androidx.appcompat.app.AppCompatActivity;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.NavigationUI;
import android.os.Bundle;
import com.google.android.material.bottomnavigation.BottomNavigationView;
public class MainActivity extends AppCompatActivity {
private BottomNavigationView bottomNavigationView;
private NavController navController;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bottomNavigationView = findViewById(R.id.bottomNavigationView);
navController = Navigation.findNavController(this, R.id.frame_layout);
NavigationUI.setupWithNavController(bottomNavigationView, navController);
}
}
main_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<fragment
android:id="@+id/frame_layout"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="0dp"
app:defaultNavHost="true"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
app:layout_constraintBottom_toTopOf="@+id/bottomNavigationView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="@navigation/mobile_navigation" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottomNavigationView"
android:layout_width="match_parent"
android:layout_height="50dp"
app:menu="@menu/bottom"
android:background="?android:attr/windowBackground"
app:labelVisibilityMode="selected"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
mobile_navigation.xml:
<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/navigation"
app:startDestination="@id/galleryFragment">
<fragment
android:id="@+id/aboutFragment"
android:name="com.example.mycollege.ui.about.aboutFragment"
android:label="about"
tools:layout="@layout/fragment_about" />
<fragment
android:id="@+id/galleryFragment"
android:name="com.example.mycollege.ui.gallery.galleryFragment"
android:label="gallery"
tools:layout="@layout/fragment_gallery" />
<fragment
android:id="@+id/homeFragment"
android:name="com.example.mycollege.ui.home.homeFragment"
android:label="home"
tools:layout="@layout/fragment_home" />
<fragment
android:id="@+id/noticeFragment"
android:name="com.example.mycollege.ui.notice.noticeFragment"
android:label="notice"
tools:layout="@layout/fragment_notice" />
<fragment
android:id="@+id/facultyFragment"
android:name="com.example.mycollege.ui.faculty.facultyFragment"
android:label="faculty"
tools:layout="@layout/fragment_faculty" />
</navigation>
menu.xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/home_navigation"
android:icon="@drawable/home"
android:iconTint="@color/white"
android:title="Home" />
<item
android:id="@+id/notice_navigation"
android:icon="@drawable/noticeboard"
android:iconTint="@color/white"
android:title="Notice" />
<item
android:id="@+id/faculty_navigation"
android:icon="@drawable/team"
android:iconTint="@color/white"
android:title="Faculty" />
<item
android:id="@+id/gallery_navigation"
android:icon="@drawable/gallery"
android:iconTint="@color/white"
android:title="Gallery" />
<item
android:id="@+id/about_navigation"
android:icon="@drawable/writing"
android:iconTint="@color/white"
android:title="About" />
</menu>
Make sure your menu items id are the same as fragments ids.
For instance, your fragment id is aboutFragment
, then your menu item's id should be aboutFragment
.