Issue
Iam using single activity navigation component structure. In host activity iam using navigation drawer and bottom navigation.
So There are 2 frgaments inside bottom navigation Fragment A and Fragment B. I need to add menu icons (search icon) for Fragment A and (chat icon) for Fragment B.
So how i can add these icons on Host Activity toolbar from fragment ?
HostActivity
private fun setupViews()
{
drawerLayout = binding.drawerLayout
// Finding the Navigation Controller
navController = findNavController(R.id.hostFragmentLanding)
// Setting Navigation Controller with the BottomNavigationView
binding.bottomNavView.setupWithNavController(navController)
appBarConfiguration = AppBarConfiguration(topLevelDestinationIds = TOP_LEVEL_DESTINATIONS, drawerLayout)
// Set up ActionBar
setSupportActionBar(binding.toolBar)
setupActionBarWithNavController(navController, appBarConfiguration)
binding.navigationView.setupWithNavController(navController)
}
override fun onSupportNavigateUp(): Boolean {
return navController.navigateUp(appBarConfiguration) || super.onSupportNavigateUp()
}
override fun onBackPressed() {
if (drawerLayout.isDrawerOpen(GravityCompat.START)) {
drawerLayout.closeDrawer(GravityCompat.START)
} else {
super.onBackPressed()
}
}
host_activity_xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:fitsSystemWindows="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:elevation="0dp"
android:fitsSystemWindows="true">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolBar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:title=""
app:theme="@style/ToolbarTheme"
>
<ImageView
android:id="@+id/logo"
android:layout_width="97dp"
android:layout_height="@dimen/dp_20"
android:src="@drawable/logo_sportsal"
android:layout_gravity="center"
/>
</androidx.appcompat.widget.Toolbar>
</com.google.android.material.appbar.AppBarLayout>
<fragment
android:id="@+id/hostFragmentLanding"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
app:navGraph="@navigation/navigation_landing" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottomNavView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_behavior="@string/hide_bottom_view_on_scroll_behavior"
style="@style/Widget.MaterialComponents.BottomNavigationView"
app:menu="@menu/bottom_navigation_menu"
/>
</LinearLayout>
<com.google.android.material.navigation.NavigationView
android:id="@+id/navigation_view"
style="@style/Widget.MaterialComponents.NavigationView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start" />
</androidx.drawerlayout.widget.DrawerLayout>
I want to add menu (search)icon from Fragment A to the Hostactivity Toolbar
Solution
I have added menu icons directlty from the fragment class
Fragment A
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
setHasOptionsMenu(true)
}
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
super.onCreateOptionsMenu(menu, inflater)
inflater.inflate(R.menu.menu_home_fragment, menu)
menu.findItem(R.id.search).isVisible = false
menu.findItem(R.id.qrScan).isVisible = false
menu.findItem(R.id.notification).isVisible = true
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
if (item.itemId == R.id.search) {
}
return super.onOptionsItemSelected(item)
}
So the menu items has been added to the Host Activities Toolbar automatically
Answered By - Navin Kumar
Answer Checked By - Marie Seifert (JavaFixing Admin)