How to start an activity on material navigation drawer menu item clicked

userdks

I am using collapsible action bar. While trying to start new activity got error: Cannot resolve constructor 'Intent(anonymous android.support.design.widget.NavigationView.OnNavigationItemSelectedListener, java.lang.Class)'

Please help me on this:

Can we use start new activity from navigation drawer menu item click ?

Or

We have only option to replace and show fragments from navigation drawer on menu item click

FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.containerView,new SentFragment()).commit();

if yes, then how can I replace tab activity and viewpager and set action bar height ?

activity_main.xml

<android.support.v4.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/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.test.app.activities.MainActivity">

<android.support.design.widget.CoordinatorLayout
    android:id="@+id/rootLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="@dimen/app_bar_height"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="48dp"
            android:minHeight="?attr/actionBarSize"

            app:layout_scrollFlags="scroll|enterAlways"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />

        <android.support.design.widget.TabLayout
            android:id="@+id/tabLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:tabIndicatorHeight="4dp"
            app:tabGravity="fill"
            app:tabMode="scrollable"
            app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />

    </android.support.design.widget.AppBarLayout>

         <android.support.v4.view.ViewPager
            android:id="@+id/viewpager"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior" />



    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fabBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_marginBottom="@dimen/fab_margin_bottom"
        android:layout_marginRight="@dimen/fab_margin_right"
        android:src="@drawable/ic_plus"

        app:borderWidth="0dp"
        app:fabSize="normal" />

</android.support.design.widget.CoordinatorLayout>

<android.support.design.widget.NavigationView
    android:id="@+id/navigation"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    app:headerLayout="@layout/nav_header"
    app:itemIconTint="@color/nav_item_icon_tint_color"
    app:itemTextColor="@color/nav_item_text_color"
    app:menu="@menu/navigation_drawer_items" />

MainActivity.java

navigation = (NavigationView) findViewById(R.id.navigation);
    navigation.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(MenuItem menuItem) {
            int id = menuItem.getItemId();
            drawerLayout.closeDrawers();
            switch (id){
                case R.id.home:
                    //do something here
                    //open new activity
                    /*unable to start activity*/
                    //Intent intent = new Intent(this, NewActivity.class);
                    /* error on above line shown: Cannot resolve constructor 'Intent(anonymous android.support.design.widget.NavigationView.OnNavigationItemSelectedListener, java.lang.Class<com.test.app.activities.RouteActivity>)'*/
                    break;
                case R.id.logout:
                   //add navigation drawer item onclick method here
                    break;

            }
            return false;
        }
    });
Michele Lacorte

According to Android Docs to start new activity you have to do:

1) Add this line in your class:

import android.content.Intent;

2) Add new activity to manifest, looks like this:

 <activity
        android:name=".YourActivityName"
        android:label="YourActivityName">
        <intent-filter>
            <action android:name="package.name.YOURACTIVITYNAME" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>

3) Start activity with startActivity() method, like this:

Intent newAct = new Intent(getApplicationContext(), YourActivityName.class);
startActivity(newAct);

3a) If your class extends Activity you can pass this instead of getApplicationContext():

Intent newAct = new Intent(this, YourActivityName.class);
startActivity(newAct);

3b) If your class extends Fragment you must use getActivity() to pass Context:

Intent newAct = new Intent(getActivity(), YourActivityName.class);
startActivity(newAct);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Start Navigation Drawer Activity when a button is clicked

From Dev

Start an Activity From material navigation drawer that has a switch case for menu items which are fragments

From Dev

Start an Activity From material navigation drawer that has a switch case for menu items which are fragments

From Dev

How to hide a navigation drawer menu item programmatically?

From Dev

Open new Activity after click item in navigation drawer menu

From Dev

How to attach layout/activitise to menu in Navigation Drawer Activity

From Dev

How to change selected Item in the navigation drawer depending on the activity/view?

From Dev

How to add a collapsible menu item inside navigation drawer in android?

From Dev

How to draw a line between menu item in navigation drawer

From Dev

How to draw a line between menu item in navigation drawer

From Dev

How to select the first item in a navigation drawer and open a fragment on application start

From Dev

Show clicked Item highlighted in Navigation Drawer Android

From Dev

Open navigation drawer menu in another activity

From Dev

Starting a new activity on navigation drawer item click

From Dev

Unable to start activity ComponentInfo with Mainactivity and Navigation drawer?

From Dev

How to start intent activity when context menu clicked?

From Dev

How can I start new Activity based on the item clicked in listview?

From Java

Change the color of a checked menu item in a navigation drawer

From Java

Changing text color of menu item in navigation drawer

From Java

Hide a Navigation Drawer Menu Item - Android

From Dev

Underline menu item in navigation drawer list

From Dev

Navigation drawer menu item with titles and sub titles

From Dev

Navigation Drawer Menu Item Title Color in Android

From Dev

Android Navigation drawer menu item icon color

From Dev

Navigation drawer menu item with titles and sub titles

From Dev

Android draw a rectangle to navigation drawer menu item

From Dev

How to open sub menu after click on menu-item in Navigation drawer?

From Dev

How do I change Item title font color for an Item that contains a menu in Navigation Drawer

From Dev

How to start different activity using switch case in the menu item?

Related Related

  1. 1

    Start Navigation Drawer Activity when a button is clicked

  2. 2

    Start an Activity From material navigation drawer that has a switch case for menu items which are fragments

  3. 3

    Start an Activity From material navigation drawer that has a switch case for menu items which are fragments

  4. 4

    How to hide a navigation drawer menu item programmatically?

  5. 5

    Open new Activity after click item in navigation drawer menu

  6. 6

    How to attach layout/activitise to menu in Navigation Drawer Activity

  7. 7

    How to change selected Item in the navigation drawer depending on the activity/view?

  8. 8

    How to add a collapsible menu item inside navigation drawer in android?

  9. 9

    How to draw a line between menu item in navigation drawer

  10. 10

    How to draw a line between menu item in navigation drawer

  11. 11

    How to select the first item in a navigation drawer and open a fragment on application start

  12. 12

    Show clicked Item highlighted in Navigation Drawer Android

  13. 13

    Open navigation drawer menu in another activity

  14. 14

    Starting a new activity on navigation drawer item click

  15. 15

    Unable to start activity ComponentInfo with Mainactivity and Navigation drawer?

  16. 16

    How to start intent activity when context menu clicked?

  17. 17

    How can I start new Activity based on the item clicked in listview?

  18. 18

    Change the color of a checked menu item in a navigation drawer

  19. 19

    Changing text color of menu item in navigation drawer

  20. 20

    Hide a Navigation Drawer Menu Item - Android

  21. 21

    Underline menu item in navigation drawer list

  22. 22

    Navigation drawer menu item with titles and sub titles

  23. 23

    Navigation Drawer Menu Item Title Color in Android

  24. 24

    Android Navigation drawer menu item icon color

  25. 25

    Navigation drawer menu item with titles and sub titles

  26. 26

    Android draw a rectangle to navigation drawer menu item

  27. 27

    How to open sub menu after click on menu-item in Navigation drawer?

  28. 28

    How do I change Item title font color for an Item that contains a menu in Navigation Drawer

  29. 29

    How to start different activity using switch case in the menu item?

HotTag

Archive