The process for defining the title in the app bar using Navigation Architecture Component involves the following steps:
Add the dependency for Navigation Architecture Component in the app build.gradle file.
Define the navigation graph in the res/navigation directory of the project.
In the layout XML file for the destination fragment, add the attribute app:defaultNavHost="true" to the NavHostFragment.
Define the app bar in the layout XML file for the activity that contains the NavHostFragment.
In the MainActivity.kt file, add an instance of the NavController and set it as the NavigationUI controller for the app bar.
In each destination fragment, set the title using the NavigationUI setTitle method.
Build and run the app to see the updated title in the app bar when navigating between fragments.
Example code snippets for some of these steps are provided below:
Step 2 - Define the navigation graph
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:startDestination="@id/homeFragment">
<fragment
android:id="@+id/homeFragment"
android:name="com.example.app.HomeFragment"
android:label="@string/home_title" />
<fragment
android:id="@+id/aboutFragment"
android:name="com.example.app.AboutFragment"
android:label="@string/about_title" />
</navigation>
Step 3 - Add attribute to NavHostFragment
<fragment
android:id="@+id/navHostFragment"
android:name="androidx.navigation.fragment.NavHostFragment"
app:navGraph="@navigation/nav_graph"
app:defaultNavHost="true"
... />
Step 4 - Define the app bar in activity layout XML
<com.google.android.material.appbar.AppBarLayout
...
>
<com.google.android.material.appbar.MaterialToolbar
android:id="@+id/appBar"
...
/>
</com.google.android.material.appbar.AppBarLayout>
Step 5 - Set NavigationUI controller for app bar in MainActivity
class MainActivity : AppCompatActivity() {
private lateinit var navController: NavController
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
navController = findNavController(R.id.navHostFragment)
val appBar = findViewById<Toolbar>(R.id.appBar)
setSupportActionBar(appBar)
NavigationUI.setupActionBarWithNavController(this, navController)
}
override fun onSupportNavigateUp(): Boolean {
return navController.navigateUp() || super.onSupportNavigateUp()
}
}
Step 6 - Set title in destination fragment
class HomeFragment : Fragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
NavigationUI.setTitle(activity, R.string.home_title)
}
...
}
Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss
Asked: 2023-04-02 11:00:00 +0000
Seen: 9 times
Last updated: Feb 15 '22
Can a transitive dependency in gradle be made exclusively for runtime purposes?
Why can't IntelliJ IDEA detect the Kotlin multiplatform library?
How do compileSdkVersion and targetSdkVersion differ from each other?
What does the error message "Failed to apply plugin [id 'com.jfrog.artifactory']" mean?
Why doesn't the Android Studio display the layout preview once MaterialCalendarView has been added?
How to update the outdated WebSecurityConfigurerAdapter in Spring Boot?
How can we add animation to the user interface using jetpack compose?