Sunny Marthak
Sunny Marthak

Reputation: 43

How to open New Activity by clicking Navigation Drawer Item in Android

How can I open New Activity by clicking a Navigation Drawer Menu Item?

Share_Home.java

public class Share_Home extends AppCompatActivity {

    private long backPressedTime;
    private ActionBarDrawerToggle toggle;
    private DrawerLayout drawer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_share_home);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        drawer = findViewById(R.id.drawer_layout);
        NavigationView navigationView = findViewById(R.id.nav_view);
        // Passing each menu ID as a set of Ids because each
        // menu should be considered as top level destinations.
        SectionsPagerAdapter sectionsPagerAdapter = new SectionsPagerAdapter(this, getSupportFragmentManager());
        ViewPager viewPager = findViewById(R.id.view_pager);
        viewPager.setAdapter(sectionsPagerAdapter);
        TabLayout tabs = findViewById(R.id.tabs);
        tabs.setupWithViewPager(viewPager);
        tabs.setTabMode(TabLayout.MODE_SCROLLABLE);
        tabs.getTabAt(1).select();
        toggle = new ActionBarDrawerToggle(this, drawer, R.string.open, R.string.close);
        drawer.addDrawerListener(toggle);
        toggle.syncState();
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (toggle.onOptionsItemSelected(item)){
            return  true;
        }
        return super.onOptionsItemSelected(item);
    }
}

activity_share_home.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:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <include
        layout="@layout/app_bar_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <com.google.android.material.navigation.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/nav_menu_item" />

    <include
        layout="@layout/activity_tabs_menu"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</androidx.drawerlayout.widget.DrawerLayout>

nav_menu_item.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/nav_Setting"
        android:icon="@drawable/setting_icon"
        android:title="@string/nav_Setting" />
    <item
        android:id="@+id/nav_Help"
        android:icon="@drawable/help_icon"
        android:title="@string/nav_Help" />
    <item
        android:id="@+id/nav_Ratings"
        android:icon="@drawable/rating_icon"
        android:title="@string/nav_Ratings" />
    <item
        android:id="@+id/nav_About"
        android:icon="@drawable/about_icon"
        android:title="@string/nav_About" />
</menu>

Upvotes: 4

Views: 5089

Answers (4)

Gabriele Mariotti
Gabriele Mariotti

Reputation: 365038

Set a NavigationItemSelectedListener to you NavigationView:

navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
             int id=menuItem.getItemId();    
             if (id == R.id.xxx){
                  Intent newIntent = new Intent(this, NewActivity.class);
                  startActivity(newIntent);  
              }
              return true;
         }
      });

Also change your layout.

Instead of:

<androidx.drawerlayout.widget.DrawerLayout >

    <include layout="@layout/app_bar_main" />
    <com.google.android.material.navigation.NavigationView/>
    <include layout="@layout/activity_tabs_menu"/>

</androidx.drawerlayout.widget.DrawerLayout>

Use:

<androidx.drawerlayout.widget.DrawerLayout >

    <include layout="@layout/app_bar_main" />
    <com.google.android.material.navigation.NavigationView/>

</androidx.drawerlayout.widget.DrawerLayout>

moving the <include layout="@layout/activity_tabs_menu"/> inside the app_bar_main layout.

Upvotes: 3

waseefakhtar
waseefakhtar

Reputation: 1423

You get a listener in your NavigationView that listens to the item pressed. So you can set the listener and then start an activity when a particular item is pressed in the NavigationDrawer, like so:

navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
      @Override
      public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
          switch (menuItem.getItemId()) { 
              case R.id.menu_item_1:
                  Intent intent = new Intent(this, ItemActivity.class);
                  startActivity(intent);  
                  break;
          }
      }
});

Upvotes: 0

matdev
matdev

Reputation: 4283

Here is how you can do it:

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    final int itemId = item.getItemId();

    if (itemId == R.id.nav_Setting) {
        Intent newIntent = new Intent(getApplicationContext(), YourSettingActivity.class);
        newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(newIntent);
    
        return true;
    }
    return super.onOptionsItemSelected(item);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.nav_menu_item, menu);

    return super.onCreateOptionsMenu(menu);
}

Upvotes: 0

iknow
iknow

Reputation: 9912

You have to override onNavigationItemSelected and check the id of the selected menu item and then open Activity:

@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
    switch (item.getItemId()) {
        case R.id.activity1:
            startActivity(new Intent(this, Activity1.class));
            break;
        case R.id.activity2:
            startActivity(new Intent(this, Activity2.class));
            break;
    }
    drawer.closeDrawer(GravityCompat.START);
    return true;
}

And remember to add id to every item in menu layout:

<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:id="@+id/activity1"
        android:title="@string/activity1" />

    <item
        android:id="@+id/activity2"
        android:title="@string/activity2" />

</menu>

Upvotes: 0

Related Questions