简体   繁体   中英

How to go another activity by clicking Navigation item?

Here I added my code. I have been created Navigation bar successfully. But Now I am not able to set onClickListener on my Navigation Item. How can I use a Intent on my Navigation Item using navigation item id. please help me someone. I need to know in details

activity_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"
    tools:context=".MainActivity"
    android:orientation="vertical"
    android:id="@+id/my_drawer_layout"
    >
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            >
            <include
                layout="@layout/main_dashboard"
                />
            <include layout="@layout/lead"
                />

        </LinearLayout>

    <com.google.android.material.navigation.NavigationView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:menu="@menu/navigation_menu" />

</androidx.drawerlayout.widget.DrawerLayout>

**navigation_menu.xml**

    <?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:ignore="HardcodedText">

    <item
        android:id="@+id/nav_account"
        android:title="My Account"
        android:icon="@drawable/user"
        />
    <item
        android:id="@+id/nav_home"
        android:title="Home"
        android:icon="@drawable/home"
        />

    <item
        android:id="@+id/nav_notification"
        android:title="Notification"
        android:icon="@drawable/notification"
        />
    <item
        android:id="@+id/nav_offer"
        android:title="Offers"
        android:icon="@drawable/offer"
        />
    <item
        android:id="@+id/nav_ticket"
        android:title="Tickets"
        android:icon="@drawable/ticket"
        />
    <item
        android:id="@+id/nav_tutorial"
        android:title="Tutorial"
        android:icon="@drawable/tutorial"
        />
    <item
        android:id="@+id/nav_privacy"
        android:title="Privacy &amp; Policy"
        android:icon="@drawable/privacy"
        />

    <item
        android:id="@+id/nav_logout"
        android:title="Logout"
        android:icon="@drawable/log_out"
        />

</menu>

MainActivity.java

     @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);



        // drawer layout instance to toggle the menu icon to open
        // drawer and back button to close drawer
        drawerLayout = findViewById(R.id.my_drawer_layout);
        actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.nav_open, R.string.nav_close);

        // pass the Open and Close toggle for the drawer layout listener
        // to toggle the button
        drawerLayout.addDrawerListener(actionBarDrawerToggle);
        actionBarDrawerToggle.syncState();

        // to make the Navigation drawer icon always appear on the action bar
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    }


  @Override
 public boolean onOptionsItemSelected(@NonNull MenuItem item) {

     if (actionBarDrawerToggle.onOptionsItemSelected(item)) {
         return true;
     }
     return super.onOptionsItemSelected(item);
 }

enter image description here

Implement this in your Activity

Step 1

public class MainActivity extends AppCompatActivity implements 
              NavigationView.OnNavigationItemSelectedListener

Step 2 setNavigationItemSelectedListener in onCreate

NavigationView mNavigationView = (NavigationView) findViewById(R.id.account_navigation_view);

if (mNavigationView != null) {
        mNavigationView.setNavigationItemSelectedListener(this);
}

Step 3 override the below 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 
    }
}

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