Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
ODG - Navigation: BottomNavigationView
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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/main"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<androidx.fragment.app.FragmentContainerView
android:id="@+id/nav_host"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
app:navGraph="@navigation/nav_root" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottomNavigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:menu="@menu/menu_navigation" />
</LinearLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val appBarConfiguration = AppBarConfiguration(setOf(R.id.nav_home, R.id.nav_screenTwo))
val navHostFragment = supportFragmentManager.findFragmentById(R.id.nav_host) as NavHostFragment
val navController = navHostFragment.navController
setupActionBarWithNavController(navController, appBarConfiguration)
bottomNavigation.setupWithNavController(navController)
}
}
<?xml version="1.0" encoding="utf-8"?>
<!--File path: app/src/main/res/menu-->
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/nav_home"
android:icon="@drawable/ic_home_24"
android:contentDescription="@string/home"
android:title="@string/title_home" />
<item
android:id="@+id/nav_screenTwo"
android:icon="@drawable/ic_screenTwo_24"
android:contentDescription="@string/screenTwo"
android:title="@string/title_screenTwo" />
</menu>
<?xml version="1.0" encoding="utf-8"?>
<!--File path: app/src/main/res/navigation-->
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/root"
app:startDestination="@id/nav_home">
<include app:graph="@navigation/nav_home" />
<include app:graph="@navigation/nav_screenTwo" />
</navigation>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment