Sushil Shinde
Sushil Shinde

Reputation: 307

How to add click event to item on NavigationView of Android

I am trying to implement Sidebar NavigationDrawer in my Android project. To do so, I have used NavigationView in DrawerLayout. To show items I used menu. I want to add click event on that added menu items.

Code for reference: In navigation menu -

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/nav_account" android:title="My Account"/>
    <item android:id="@+id/nav_settings" android:title="Settings"/>
    <item android:id="@+id/nav_layout" android:title="Log Out"/>
</menu>

In View:

<android.support.design.widget.NavigationView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        app:menu="@menu/navigation_menu"
        android:layout_gravity="start" />

Upvotes: 13

Views: 25597

Answers (4)

SamPhoenix
SamPhoenix

Reputation: 99

I wanted to make sure the Solution from Nizam can be found in Kotlin to, since it is takeing bigger place every day:

val mDrawerLayout = this.findViewById(R.id.drawer_layout) as DrawerLayout

val mNavigationView = findViewById<View>(R.id.navigation) as NavigationView

Handle the navigation items in onCreate like this:

mNavigationView.setNavigationItemSelectedListener { it: MenuItem ->
                when (it.itemId) {
                    R.id.nav_item1 -> doThis()
                    R.id.nav_item2-> doThat()
                    else -> {
                        true
                    }
                }
            }

Remember: Return Type has to be a boolean!

Upvotes: 3

Nizam
Nizam

Reputation: 5731

  1. Implement the listener in your Activity:

    public class HomeActivity extends AppCompatActivity implements 
                  NavigationView.OnNavigationItemSelectedListener
    
  2. setNavigationItemSelectedListener in onCreate of Activity

    NavigationView mNavigationView = (NavigationView) findViewById(R.id.account_navigation_view);
    
    if (mNavigationView != null) {
            mNavigationView.setNavigationItemSelectedListener(this);
    }
    
  3. Override the method

    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();
    
        if (id == R.id.nav_account) {
           // DO your stuff 
        }
    }
    

Upvotes: 45

Zon
Zon

Reputation: 19880

Here is a Java 8 approach with smaller boilerplate (no "implements" on Activity). Also helpful if your class is abstract and you don't want to implement this functionality in every other child:

@Override
protected void onCreate(
  Bundle savedInstanceState) {

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

  navigationView.setNavigationItemSelectedListener(
    MyActivity::onNavigationItemSelected);
}

public static boolean onNavigationItemSelected(MenuItem item) {

  if (item.getId() == R.id.my_item) {
    myItemClickHandler();
  }

  return false;
}

Upvotes: 0

iSandeep
iSandeep

Reputation: 715

You have to use OnNavigationItemSelectedListener(MenuItem item) method.

for more check this documentation.

Upvotes: 2

Related Questions