The drawer opens fine and displayed correctly but nothing happens when I click on the items in the list. Here is my code that is taken from the google tutorials (UDACITY).
activity_main.xml
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<androidx.drawerlayout.widget.DrawerLayout
android:id="@+id/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<fragment
android:id="@+id/myNavHostFragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:navGraph="@navigation/navigation"
/>
</LinearLayout>
<com.google.android.material.navigation.NavigationView
android:id="@+id/navView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:menu="@menu/navdrawer_menu"
app:headerLayout="@layout/nav_header"/>
</androidx.drawerlayout.widget.DrawerLayout>
MainActivity.kt
class MainActivity : AppCompatActivity() {
private lateinit var drawerLayout: DrawerLayout
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
@Suppress("UNUSED_VARIABLE")
val binding = DataBindingUtil.setContentView<ActivityMainBinding>(this, R.layout.activity_main)
drawerLayout = binding.drawerLayout
val navController = this.findNavController(R.id.myNavHostFragment)
NavigationUI.setupActionBarWithNavController(this, navController, drawerLayout)
NavigationUI.setupWithNavController(binding.navView, navController)
}
override fun onSupportNavigateUp(): Boolean {
val navController = this.findNavController(R.id.myNavHostFragment)
return NavigationUI.navigateUp(navController, drawerLayout)
navigation.xml
I wish it helps you.
<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/navigation"
app:startDestination="@id/titleFragment">
<fragment
android:id="@+id/titleFragment"
android:name="com.example.android.navigation.TitleFragment"
android:label="fragment_title"
tools:layout="@layout/fragment_title" >
<action
android:id="@+id/action_titleFragment_to_gameFragment"
app:destination="@id/gameFragment" />
</fragment>
<fragment
android:id="@+id/gameFragment"
android:name="com.example.android.navigation.GameFragment"
android:label="fragment_game"
tools:layout="@layout/fragment_game" >
<action
android:id="@+id/action_gameFragment_to_gameOverFragment2"
app:destination="@id/gameOverFragment2"
app:popUpTo="@id/gameFragment"
app:popUpToInclusive="true" />
<action
android:id="@+id/action_gameFragment_to_gameWonFragment"
app:destination="@id/gameWonFragment"
app:popUpTo="@id/gameFragment"
app:popUpToInclusive="true" />
</fragment>
<fragment
android:id="@+id/gameOverFragment2"
android:name="com.example.android.navigation.GameOverFragment"
android:label="fragment_game_over"
tools:layout="@layout/fragment_game_over" >
<action
android:id="@+id/action_gameOverFragment_to_gameFragment"
app:destination="@id/gameFragment"
app:popUpTo="@id/titleFragment" />
</fragment>
<fragment
android:id="@+id/gameWonFragment"
android:name="com.example.android.navigation.GameWonFragment"
android:label="fragment_game_won"
tools:layout="@layout/fragment_game_won" >
<action
android:id="@+id/action_gameWonFragment_to_gameFragment"
app:destination="@id/gameFragment"
app:popUpTo="@id/titleFragment" />
<argument
android:name="numQuestions"
app:argType="integer" />
<argument
android:name="numCorrect"
app:argType="integer" />
</fragment>
<fragment
android:id="@+id/aboutFragment"
android:name="com.example.android.navigation.AboutFragment"
android:label="fragment_about"
tools:layout="@layout/fragment_about" />
<fragment
android:id="@+id/rulesFragment"
android:name="com.example.android.navigation.RulesFragment"
android:label="fragment_rules"
tools:layout="@layout/fragment_rules" />
navdrawer_menu.xml
I did everything exactly like the tutorial video.
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/rulesfragment"
android:icon="@drawable/rules"
android:title="@string/rules" />
<item
android:id="@+id/aboutfragment"
android:icon="@drawable/about_android_trivia"
android:title="@string/about" />