Skip to content

Instantly share code, notes, and snippets.

@J-Swift
Last active August 3, 2022 13:15
Show Gist options
  • Save J-Swift/f36ed34503e45a4497e24b57b0fe6cfa to your computer and use it in GitHub Desktop.
Save J-Swift/f36ed34503e45a4497e24b57b0fe6cfa to your computer and use it in GitHub Desktop.
(Almost) fully programmatic creation of Jetpack Navigation graph
<?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"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="?attr/actionBarSize">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/nav_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="0dp"
android:layout_marginEnd="0dp"
android:background="?android:attr/windowBackground"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" />
<androidx.fragment.app.FragmentContainerView
android:id="@+id/nav_host_fragment_activity_main"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:layout_constraintBottom_toTopOf="@id/nav_view"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
package com.example.myapplication
import android.os.Bundle
import android.util.Log
import android.view.Menu
import com.google.android.material.bottomnavigation.BottomNavigationView
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentContainerView
import androidx.navigation.NavGraph
import androidx.navigation.NavGraphBuilder
import androidx.navigation.createGraph
import androidx.navigation.fragment.FragmentNavigator
import androidx.navigation.fragment.FragmentNavigatorDestinationBuilder
import androidx.navigation.fragment.NavHostFragment
import androidx.navigation.ui.AppBarConfiguration
import androidx.navigation.ui.setupActionBarWithNavController
import androidx.navigation.ui.setupWithNavController
import com.example.myapplication.ui.dashboard.DashboardFragment
import com.example.myapplication.ui.home.HomeFragment
import com.example.myapplication.ui.notifications.NotificationsFragment
import kotlin.reflect.KClass
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val navView = findViewById<BottomNavigationView>(R.id.nav_view)
val navHost = supportFragmentManager.findFragmentById(R.id.nav_host_fragment_activity_main) as NavHostFragment
val navController = navHost.navController
val navigator = navController.navigatorProvider.getNavigator(FragmentNavigator::class.java)
val destFor = { klass: KClass<out Fragment>, id: String, lbl: String ->
with(FragmentNavigatorDestinationBuilder(navigator, id, klass)) {
label = lbl
build()
}
}
val dests = arrayOf(
destFor(DashboardFragment::class, "navigation_dash", "Dashboard"),
destFor(HomeFragment::class, "navigation_home", "Home"),
destFor(NotificationsFragment::class, "navigation_notifications", "Notifications"),
)
val graphBuilder = NavGraphBuilder(navController.navigatorProvider, dests[0].route!!, null)
dests.forEach {
graphBuilder.addDestination(it)
navView.menu.add(Menu.NONE, it.id, Menu.NONE, it.label)
}
val graph = graphBuilder.build()
navController.setGraph(graph, null)
val appBarConfiguration = AppBarConfiguration(dests.map { it.id }.toSet())
setupActionBarWithNavController(navController, appBarConfiguration)
navView.setupWithNavController(navController)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment