I am working with Androidx, android jetpack navigation with navigation drawer not display back button and title given in the mobile_navigation.xml, navigation i have tried with nav_view.setupWithNavController(navController) is not working, studio shows unresolved symbol. I have added required dependency for the project
Gradle.build :
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.qbitstream.salesmanagementsystem"
minSdkVersion 19
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
def nav_version = "1.0.0-alpha04"
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.0.0-rc01'
implementation 'androidx.constraintlayout:constraintlayout:1.1.2'
implementation 'androidx.lifecycle:lifecycle-extensions:2.0.0-rc01'
implementation 'androidx.legacy:legacy-support-v4:1.0.0-rc01'
implementation 'com.google.android.material:material:1.0.0-alpha1'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.1.0-alpha4'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0-alpha4'
implementation "android.arch.navigation:navigation-fragment:$nav_version" // use -ktx for Kotlin
implementation "android.arch.navigation:navigation-ui:$nav_version"
def room_version = "2.0.0-rc01"
implementation "androidx.room:room-runtime:$room_version"
kapt "androidx.room:room-compiler:$room_version"
implementation "androidx.room:room-rxjava2:$room_version"
implementation "androidx.room:room-guava:$room_version"
testImplementation "androidx.room:room-testing:$room_version"
implementation "com.squareup.retrofit2:retrofit:2.3.0"
implementation "com.squareup.retrofit2:adapter-rxjava2:2.3.0"
implementation "com.squareup.retrofit2:converter-gson:2.3.0"
implementation "io.reactivex.rxjava2:rxandroid:2.0.1"
implementation 'org.jetbrains.anko:anko-common:0.9'
}
This is my HomeActivity:
import android.os.Bundle
import androidx.appcompat.app.ActionBarDrawerToggle
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.GravityCompat
import androidx.navigation.fragment.NavHostFragment
import androidx.navigation.ui.NavigationUI
import kotlinx.android.synthetic.main.activity_home.*
import kotlinx.android.synthetic.main.app_bar_home.*
class HomeActivity : AppCompatActivity() {
private lateinit var navController:NavController
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_home)
setSupportActionBar(toolbar)
val toggle = ActionBarDrawerToggle(this, drawer_layout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close)
drawer_layout.addDrawerListener(toggle)
toggle.syncState()
val host = supportFragmentManager.findFragmentById(R.id.m_fragment) as NavHostFragment? ?: return
navController = host.navController
NavigationUI.setupWithNavController(nav_view,navController)
}
override fun onBackPressed() {
if (drawer_layout.isDrawerOpen(GravityCompat.START)) {
drawer_layout.closeDrawer(GravityCompat.START)
} else {
super.onBackPressed()
}
}
override fun onSupportNavigateUp(): Boolean {
return NavigationUI.navigateUp(drawer_layout, navController) || super.onSupportNavigateUp()
}
} drawer_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:showIn="navigation_view">
<group android:checkableBehavior="single">
<item
android:id="@+id/homeFragment"
android:icon="@drawable/order"
android:title="Home" />
<item
android:id="@+id/salesOrderFragment"
android:icon="@drawable/order"
android:title="Order" />
<item
android:id="@+id/nav_gallery"
android:icon="@drawable/bill"
android:title="Bill Collection" />
<item
android:id="@+id/nav_slideshow"
android:icon="@drawable/bill"
android:title="Sales History" />
<item
android:id="@+id/logoutFragment"
android:icon="@drawable/order"
android:title="Logout" />
</group>
</menu>
This is mobile_navigation.xml
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_nav"
app:startDestination="@+id/homeFragment">
<fragment
android:id="@+id/homeFragment"
android:name="com.qbitstream.salesmanagementsystem.ui.home.HomeFragment"
android:label="Home"
tools:layout="@layout/fragment_home"/>
<fragment
android:id="@+id/salesOrderFragment"
android:name="com.qbitstream.salesmanagementsystem.ui.order.SalesOrderFragment"
android:label="Order"
tools:layout="@layout/fragment_sales_order" />
<fragment
android:id="@+id/logoutFragment"
android:name="com.qbitstream.salesmanagementsystem.LogoutFragment"
android:label="fragment_logout"
tools:layout="@layout/fragment_logout" />
</navigation>
Need to use both these functions
NavigationUI.setupWithNavController (toolbar, navController, drawer_layout)
NavigationUI.setupWithNavController(nav_view,navController)
You need to use NavigationUI.setupActionBarWithNavController()
Sets up the ActionBar returned by AppCompatActivity.getSupportActionBar()
for use with a NavController
.
By calling this method, the title in the action bar will automatically be updated when the destination changes (assuming there is a valid label).
The actionBar
will also display the Up button
when you are on a non-root destination. Call navigateUp(DrawerLayout, NavController)
to handle the Up button
SAMPLE CODE
For Kotlin
NavigationUI.setupActionBarWithNavController(this, navHost.navController)
For Java
NavigationUI.setupActionBarWithNavController(this, navController.getNavController());
EDIT
Don't forgot to override
onSupportNavigateUp()
method
fun onSupportNavigateUp(): Boolean {
// return navHost.navController.navigateUp() || super.onSupportNavigateUp()
return NavigationUI.navigateUp(drawer, navHost.navController) || super.onSupportNavigateUp()
}
EDIT 2
To use with navigation drawer You need to use NavigationUI.setupWithNavController()
For Kotlin
NavigationUI.setupWithNavController(toolbar, navController, drawer_layout)
For Java
NavigationUI.setupWithNavController(toolbar, navController.getNavController(),drawer_layout);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With