Martin
Martin

Reputation: 227

Setting OnClick for NavigationDrawer Items in android

I have made a navigation drawer with some 20-25 items in them. Its path is res/menu/navigation_menu.xml. In my Main Activity I wrote the below code which in my best knowledge till now is the default code for navigation drawers:

findViewById(R.id.imageMenu).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                drawerLayout.openDrawer(GravityCompat.START);
            }
        });

        NavigationView navigationView = findViewById(R.id.navigationView);
        navigationView.setItemIconTintList(null);

        NavController navController = Navigation.findNavController(this, R.id.navHostFragment);
        NavigationUI.setupWithNavController(navigationView, navController);

        navController.addOnDestinationChangedListener(new NavController.OnDestinationChangedListener() {
            @Override
            public void onDestinationChanged(@NonNull NavController controller, @NonNull NavDestination destination, @Nullable Bundle arguments) {
                tv1.setText(destination.getLabel());
            }
        });

here 'R.id.imageMenu' is that 3 horizontal lined image which opens the drawer. This is my activity_main.xml file:

<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout
    android:id="@+id/drawerLayout"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="55dp"
            android:background="#0097A7">

            <ImageView
                android:id="@+id/imageMenu"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:layout_marginStart="20dp"
                android:src="@drawable/ic_menu_white_24dp">
            </ImageView>

            <TextView
                android:id="@+id/mainTextView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="All docs"
                android:textSize="18sp"
                android:layout_centerVertical="true"
                android:layout_marginStart="55dp"
                android:textColor="#FFFFFF">
            </TextView>

            <ImageButton
                android:id="@+id/mbib"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:layout_alignParentRight="true"
                android:layout_marginRight="20dp"
                android:background="@drawable/ic_more_vert_white_24dp">
            </ImageButton>

        </RelativeLayout>

        <fragment
            android:id="@+id/navHostFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="55dp"
            android:name="androidx.navigation.fragment.NavHostFragment"
            app:defaultNavHost="true"
            app:navGraph="@navigation/main">
        </fragment>

        <androidx.cardview.widget.CardView
            android:id="@+id/cv1"
            android:visibility="invisible"
            android:layout_width="250dp"
            android:layout_height="130dp"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:layout_marginTop="18dp"
            android:layout_marginRight="25dp"
            app:cardCornerRadius="7.5dp"
            android:background="#47433F">

            <RelativeLayout
                android:id="@+id/mbrl1"
                android:layout_width="match_parent"
                android:background="?android:attr/selectableItemBackground"
                android:layout_height="65dp">

                <TextView
                    android:id="@+id/mbtv1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="25dp"
                    android:layout_marginLeft="28dp"
                    android:drawableLeft="@drawable/ic_image_white_24dp"
                    android:textColor="#FFFFFF"
                    android:textSize="15sp"
                    android:text="      Import from Gallery">
                </TextView>

            </RelativeLayout>

            <RelativeLayout
                android:id="@+id/mbrl2"
                android:layout_width="match_parent"
                android:background="?android:attr/selectableItemBackground"
                android:layout_height="65dp"
                android:layout_marginTop="65dp">

                <TextView
                    android:id="@+id/mbtv2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="10dp"
                    android:layout_marginLeft="28dp"
                    android:drawableLeft="@drawable/ic_picture_as_pdf_white_24dp"
                    android:textColor="#FFFFFF"
                    android:textSize="15sp"
                    android:text="      Import PDF">
                </TextView>

            </RelativeLayout>

        </androidx.cardview.widget.CardView>

    </RelativeLayout>

    <com.google.android.material.navigation.NavigationView
        android:id="@+id/navigationView"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        app:menu="@menu/navigation_menu"
        app:headerLayout="@layout/layout_navigation_header"
        android:layout_gravity="start">
    </com.google.android.material.navigation.NavigationView>

</androidx.drawerlayout.widget.DrawerLayout>

In my navigation drawer items, I have 2 items named Rate Us and Share for which I have not defined any fragment because for them I want to do some other work (like for Rate Us I will be opening my app link in playstore).

My Question: I want that my navigation drawer should work as it is working now except when Rate Us or Share item is clicked. In that case (it is not doing anything as there in no fragment for them) I want to open my app link in playstore or some other work. How can I achieve this? Thank You.

If I am missing on any part of code then please tell me I will update my question.

Upvotes: 2

Views: 1315

Answers (2)

Gabriele Mariotti
Gabriele Mariotti

Reputation: 363459

You can use a custom NavigationItemSelectedListener but by calling setNavigationItemSelectedListener, you are removing the original listener.

In this case you have to trigger the default behavior by calling NavigationUI.onNavDestinationSelected().

Something like:

navigationView.setNavigationItemSelectedListener {
            when (it.itemId) {
                R.id.xxxxx -> {
                    // handle click
                    true
                }
            }

             NavigationUI.onNavDestinationSelected(it, navController)
             drawerLayout.closeDrawer(GravityCompat.START)
             true
        }

Upvotes: 4

Wini
Wini

Reputation: 1986

okay for rate && Share

   @Override
public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    int id = item.getItemId();

    if (id == R.id.rateus) {
     Intent rateIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + context.getPackageName())); 
      startActivity(rateIntent);


    } else if (id == R.id.share) {
     //just an example
     String message = "Text I want to share.";
     Intent share = new Intent(Intent.ACTION_SEND);
     share.setType("text/plain");
    share.putExtra(Intent.EXTRA_TEXT, message);

    startActivity(Intent.createChooser(share, "Title of the dialog the system will open"));


    } else if (id == R.id.anything) {

    }
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}

Upvotes: 1

Related Questions