how to access views which are inside Navigation view in navigation drawer in kotlin

39 Views Asked by At

i want go back functionality in navigation view. i would like to know how to add on click listener to go back button which is located inside my navigation view. i have code like below.

my activity main

<androidx.drawerlayout.widget.DrawerLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/design_default_color_primary_dark"
    android:fitsSystemWindows="true"
    tools:openDrawer="start"
    android:id="@+id/drawer"
    tools:context=".MainActivity">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <ImageView
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:src="@drawable/baseline_supervised_user_circle_24"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            android:paddingTop="10dp"
            android:paddingLeft="10dp"
            android:id="@+id/drawer_icon"
            />

    </androidx.constraintlayout.widget.ConstraintLayout>

    <include android:id="@+id/include_drawr"
        layout="@layout/nav_drawer"
        tools:ignore="InvalidId" />

</androidx.drawerlayout.widget.DrawerLayout>

my nav_drawer.xml

<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.navigation.NavigationView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/nav_drawer"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    app:headerLayout="@layout/nav_header"
    tools:menu="@menu/nav_menu">

</com.google.android.material.navigation.NavigationView>

my nav_header.xml

<androidx.constraintlayout.widget.ConstraintLayout 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:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/go_back"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        tools:layout_editor_absoluteX="131dp"
        tools:layout_editor_absoluteY="60dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toTopOf="@+id/user"
        android:paddingTop="20dp"
        android:paddingLeft="20dp"
        android:src="@drawable/baseline_keyboard_backspace_24" />
    <ImageView
        android:id="@+id/user"
        android:paddingTop="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        tools:layout_editor_absoluteX="131dp"
        tools:layout_editor_absoluteY="60dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/go_back"
        android:src="@drawable/profile" />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="The User Name"
        app:layout_constraintTop_toBottomOf="@+id/user"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        android:textAlignment="center"
        android:textSize="18sp"
        android:layout_marginTop="10dp"
        />
</androidx.constraintlayout.widget.ConstraintLayout>

My MainActivity.kt

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val drawerIcon = findViewById<DrawerLayout>(R.id.drawer)
        val drawerImgIcon = findViewById<ImageView>(R.id.drawer_icon)

        drawerImgIcon.setOnClickListener {
            drawerIcon.openDrawer(GravityCompat.START)
        }
        val navigationView: NavigationView =  findViewById(R.id.nav_drawer)
        val header: View = navigationView.getHeaderView(0)
        val goBack: ImageView = header.findViewById(R.id.go_back)

        goBack.setOnClickListener {
            Toast.makeText(this, "Go back button fired.", Toast.LENGTH_SHORT).show()
        }
    }

}

i tried above code. it throw error. i don't know what mistake i have made in my code.

the error that i got is below

Process: com.example.navigationdrawer, PID: 7041 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.navigationdrawer/com.example.navigationdrawer.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View com.google.android.material.navigation.NavigationView.getHeaderView(int)' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3449) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601) at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85) at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:223) at android.app.ActivityThread.main(ActivityThread.java:7656) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View com.google.android.material.navigation.NavigationView.getHeaderView(int)' on a null object reference at com.example.navigationdrawer.MainActivity.onCreate(MainActivity.kt:33) at android.app.Activity.performCreate(Activity.java:7994) at android.app.Activity.performCreate(Activity.java:7978) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3422) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)  at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)  at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)  at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)  at android.os.Handler.dispatchMessage(Handler.java:106)  at android.os.Looper.loop(Looper.java:223)  at android.app.ActivityThread.main(ActivityThread.java:7656)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)

1

There are 1 best solutions below

0
coppersmith On

Hi all i rectified my mistake. what i did was that i deleted the include tag and replaced navigation tag which was in the include tag into main Activity xml file. everything works fine now.

<androidx.drawerlayout.widget.DrawerLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/design_default_color_primary_dark"
    android:fitsSystemWindows="true"
    tools:openDrawer="start"
    android:id="@+id/drawer"
    tools:context=".MainActivity">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <ImageView
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:src="@drawable/baseline_supervised_user_circle_24"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            android:paddingTop="10dp"
            android:paddingLeft="10dp"
            android:id="@+id/drawer_icon"
            />

    </androidx.constraintlayout.widget.ConstraintLayout>

    <com.google.android.material.navigation.NavigationView xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/nav_drawer"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header"
        tools:menu="@menu/nav_menu">

    </com.google.android.material.navigation.NavigationView>