Skip to content

Instantly share code, notes, and snippets.

@widiarifki
Created March 5, 2021 07:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save widiarifki/55e240a86fb10066ddb5b8d229c56014 to your computer and use it in GitHub Desktop.
Save widiarifki/55e240a86fb10066ddb5b8d229c56014 to your computer and use it in GitHub Desktop.
Jetpack Navigation with BottomNavigationView
<!-- Will be the UI entry point -->
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- FragmentContainerView -->
<androidx.fragment.app.FragmentContainerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="@dimen/bottomnav_height"
android:id="@+id/navHostFragment"
android:name="androidx.navigation.fragment.NavHostFragment"
app:defaultNavHost="true"
app:navGraph="@navigation/nav_graph" />
<!-- BottomNavigationView -->
<com.google.android.material.bottomnavigation.BottomNavigationView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="@android:color/white"
app:menu="@menu/bottomnav_menu"
app:itemIconSize="@dimen/_16sdp"
android:id="@+id/bottomMenu"/>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<!-- The menu item id, should be same with destination id in nav_gragh -->
<item
android:id="@id/browseFragment"
android:icon="@drawable/ic_search"
android:title="@string/label_browse" />
<item
android:id="@+id/trackListFragment"
android:icon="@drawable/ic_list_alt"
android:title="@string/label_tracklist" />
<item
android:id="@+id/feedsFragment"
android:icon="@drawable/ic_chat_bubble"
android:title="@string/label_feeds" />
<item
android:id="@+id/accountFragment"
android:icon="@drawable/ic_account"
android:title="@string/label_account" />
</menu>
/** Launcher activity **/
package id.widiarifki.kitsu.presentation
import android.os.Bundle
import androidx.navigation.fragment.NavHostFragment
import androidx.navigation.ui.setupWithNavController
import com.google.android.material.bottomnavigation.BottomNavigationView
import dagger.hilt.android.AndroidEntryPoint
import id.widiarifki.kitsu.R
import id.widiarifki.kitsu.base.ui.activity.NonBindingActivity
class MainActivity : NonBindingActivity() {
override val resourceLayout: Int
get() = R.layout.activity_main
override fun onViewReady(savedInstanceState: Bundle?) {
setupBottomNavMenu()
}
private fun setupBottomNavMenu() {
val host = supportFragmentManager.findFragmentById(R.id.navHostFragment) as NavHostFragment? ?: return
val navController = host.navController
val bottomNav = findViewById<BottomNavigationView>(R.id.bottomMenu)
bottomNav.setupWithNavController(navController)
}
}
<?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"
android:id="@+id/nav_graph"
app:startDestination="@id/browseFragment">
<!-- TAB 1 (default tab) -->
<fragment
android:id="@+id/browseFragment"
android:name="id.widiarifki.kitsu.presentation.anime.browse.BrowseAnimeFragment"
android:label="@string/label_browse" />
<!-- TAB 2 -->
<fragment
android:id="@+id/trackListFragment"
android:name="id.widiarifki.kitsu.presentation._temp.ComingSoonFragment"
android:label="@string/label_tracklist" />
<!-- TAB 3 -->
<fragment
android:id="@+id/feedsFragment"
android:name="id.widiarifki.kitsu.presentation._temp.ComingSoonFragment"
android:label="@string/label_feeds" />
<!-- TAB 4 -->
<fragment
android:id="@+id/accountFragment"
android:name="id.widiarifki.kitsu.presentation._temp.ComingSoonFragment"
android:label="@string/label_account" />
</navigation>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment