简体   繁体   中英

OnClick item in NavigationDrawer in Android Studio

I know this is common to ask but I really confused about how can I apply onClickListener to my navigationDrawer items, I have two items which is edit profile and Logout I just want to Toast or print if one of them is selected, Is there anyone can help me with this, I've already search on the internet , yet there's no OnClickListener I've found, need help

I think there's missing on my MainActivity which is the onCreate()

在此处输入图像描述

OnCreate() MainActivity

    setContentView(R.layout.dashboard_main);
    DrawerLayout drawerLayout = findViewById(R.id.drawerlayout);
    NavigationView navigationView = (NavigationView) findViewById(R.id.navigationview);

sidemenu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior = "single">
    <item android:id="@+id/editProfile"
        android:icon="@drawable/history"
        android:title="Edit Profile"/>

    <item android:id="@+id/logout_menu"
        android:icon="@drawable/logout"
        android:title="Logout"/>
</group>

sidebar_header

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="180dp"
    android:background="@color/dashboard_item_1"
    android:orientation="vertical"
    android:gravity="center"
    android:padding="20dp"
    >
    <de.hdodenhof.circleimageview.CircleImageView
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/profile_image"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:src="@drawable/user_logo"
        app:civ_border_width="2dp"
        app:civ_border_color="#FFFFFF"/>
    <TextView
        android:id="@+id/fullName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="User"
        android:textColor="#FFFFFF"
        android:textSize="15sp"
        android:textStyle="bold"/>

    <TextView
        android:id="@+id/idNumber"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Id no."
        android:textColor="#FFFFFF"
        android:textSize="12sp"/>


</LinearLayout>

dashboard_main.xml

    <?xml version="1.0" encoding="utf-8"?>
   <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/background"
    android:id="@+id/drawerlayout"
    tools:context=".MainActivity">

    <com.google.android.material.navigation.NavigationView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:id="@+id/navigationview"
        app:headerLayout="@layout/sidebar_header"
        app:menu="@menu/sidemenu"
        app:layout_constraintLeft_toLeftOf="parent"
        android:layout_gravity="start"/>


    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/background"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            app:title="Toolbars"
            app:titleTextColor="#FFFFFF"
            app:titleMarginStart = "10dp"
            android:layout_marginBottom="5dp"
            tools:ignore="MissingConstraints"/>


        <View
            android:id="@+id/viewHeaderBackground"
            android:layout_width="match_parent"
            android:layout_height="@dimen/_125sdp"
            android:background="@color/primary"
            app:layout_constraintTop_toTopOf="parent"/>

        <TextView
            android:id="@+id/textTitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="@dimen/_16sdp"
            android:layout_marginLeft="@dimen/_16sdp"
            android:layout_marginTop="@dimen/_16sdp"
            android:text="Dashboard"
            android:textColor="@color/white"
            android:textSize="@dimen/_20ssp"
            android:textStyle="bold"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintStart_toStartOf="parent"/>


        <androidx.appcompat.widget.AppCompatImageView
            android:layout_width="@dimen/_26sdp"
            android:layout_height="@dimen/_26sdp"
            android:layout_marginEnd="@dimen/_16sdp"
            android:layout_marginRight="@dimen/_16sdp"
            android:src="@drawable/user_logo"
            app:layout_constraintBottom_toBottomOf="@id/textTitle"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="@id/textTitle"
            />


        <com.google.android.material.card.MaterialCardView
            android:id="@+id/cardHeader"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="@dimen/_16sdp"
            android:layout_marginEnd="@dimen/_16sdp"
            app:cardBackgroundColor="@color/card_background"
            app:cardCornerRadius="@dimen/_16sdp"
            app:layout_constraintBottom_toBottomOf="@id/viewHeaderBackground"
            app:layout_constraintTop_toBottomOf="@id/viewHeaderBackground">
            <androidx.constraintlayout.widget.ConstraintLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:padding="@dimen/_14sdp">
                <TextView
                    android:id="@+id/textView"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Overview"
                    android:textColor="@color/primary_text"
                    android:textSize="@dimen/_14ssp"
                    android:textStyle="bold"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent"/>

                <ImageButton
                    android:id="@+id/textViews"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="2"
                    android:background="@android:color/transparent"
                    android:layout_marginTop="@dimen/_8sdp"
                    android:scaleType="fitCenter"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintTop_toTopOf="parent"/>
                <LinearLayout
                    android:id="@+id/layoutClients"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="@dimen/_8sdp"
                    android:gravity="center"
                    android:orientation="vertical"
                    app:layout_constraintEnd_toStartOf="@id/layoutImpacted"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toBottomOf="@id/textViews">

                    <androidx.appcompat.widget.AppCompatImageView
                        android:layout_width="@dimen/_28sdp"
                        android:layout_height="@dimen/_28sdp"
                        android:src="@drawable/ic_clients"/>

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="@dimen/_4sdp"
                        android:text="Pending"
                        android:textColor="@color/secondary_text"
                        android:textSize="@dimen/_10ssp"/>

                    <TextView
                        android:id="@+id/txtPending"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="@dimen/_4sdp"
                        android:text="0"
                        android:textColor="@color/primary_text"
                        android:textSize="@dimen/_16ssp"/>
                </LinearLayout>

                <LinearLayout
                    android:id="@+id/layoutImpacted"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="@dimen/_8sdp"
                    android:gravity="center"
                    android:orientation="vertical"
                    app:layout_constraintEnd_toStartOf="@id/layoutFollowing"
                    app:layout_constraintStart_toEndOf="@id/layoutClients"
                    app:layout_constraintTop_toBottomOf="@id/textViews">

                    <androidx.appcompat.widget.AppCompatImageView
                        android:layout_width="@dimen/_28sdp"
                        android:layout_height="@dimen/_28sdp"
                        android:src="@drawable/ic_impacted"/>

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="@dimen/_4sdp"
                        android:text="Approved"
                        android:textColor="@color/secondary_text"
                        android:textSize="@dimen/_10ssp"/>

                    <TextView
                        android:id="@+id/txtApproved"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="@dimen/_4sdp"
                        android:text="0"
                        android:textColor="@color/primary_text"
                        android:textSize="@dimen/_16ssp"/>
                </LinearLayout>

                <LinearLayout
                    android:id="@+id/layoutFollowing"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="@dimen/_8sdp"
                    android:gravity="center"
                    android:orientation="vertical"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toEndOf="@id/layoutImpacted"
                    app:layout_constraintTop_toBottomOf="@id/textViews">

                    <androidx.appcompat.widget.AppCompatImageView
                        android:layout_width="@dimen/_28sdp"
                        android:layout_height="@dimen/_28sdp"
                        android:src="@drawable/ic_following"/>

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="@dimen/_4sdp"
                        android:text="Records"
                        android:textColor="@color/secondary_text"
                        android:textSize="@dimen/_10ssp"/>

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="@dimen/_4sdp"
                        android:text="0"
                        android:textColor="@color/primary_text"
                        android:textSize="@dimen/_16ssp"/>
                </LinearLayout>
            </androidx.constraintlayout.widget.ConstraintLayout>
        </com.google.android.material.card.MaterialCardView>

        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:clipToPadding="false"
            android:overScrollMode="never"
            android:padding="@dimen/_16sdp"
            android:scrollbars="none"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintTop_toBottomOf="@id/cardHeader">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">

                <include layout="@layout/dashboard_item_1"/>
                <include layout="@layout/dashboard_item_2"/>
                <include layout="@layout/dashboard_item_3"/>
                <include layout="@layout/dashboard_item_4"/>
            </LinearLayout>

        </ScrollView>

    </androidx.constraintlayout.widget.ConstraintLayout>

</androidx.drawerlayout.widget.DrawerLayout >

Updated I tried the code below but when I click the items it will automatically hide back the sidebar and there are no Toast pops

   navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(MenuItem item) {

            if (item.getItemId() == R.id.editProfile)
            {
                Toast.makeText(getApplicationContext(),"profile",Toast.LENGTH_SHORT).show();
            }
            else if (item.getItemId() == R.id.logout_menu)
            {
                Toast.makeText(getApplicationContext(),"logout",Toast.LENGTH_SHORT).show();
            }

            return false;
        }
    });

First you need to implement NavigationView.OnNavigationItemSelectedListener interface on your Activity.
then on your onCreate() method type navigationView.setNavigationItemSelectedListener(this); after this override the method onNavigationItemSelected.
here you can use this sample code.

 @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        if (item.getItemId() == R.id.editProfile)
        {
         //edit your profile
        }
        else if (item.getItemId() == R.id.logout)
        {
              //log out
        }
        return false;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM