12

I made a NavigationView inside a DrawerLayout in an XML file - but when I try to initialize it in Java I get this error.

// DrawerLayout initialises fine
DrawerLayout drawerLayout = (DrawerLayout) findViewById(R.id.nav_drawer);
// Cannot resolve symbol 'NavigationView'
NavigationView navView = (NavigationView) findViewById(R.id.navigation_view);

I tried adding the import line

import  android.support.design.widget.NavigationView

but this is also giving me errors. Any ideas? Haven't been able to find a solution online.

Edit: After adding dependency given in Tanis' answer, I get these errors

Error:A problem occurred configuring project ':app'.
> Could not resolve all dependencies for configuration ':app:_debugCompile'.
   > Could not find com.android.support:design:22.2.1.
     Searched in the following locations:
         https://jcenter.bintray.com/com/android/support/design/22.2.1/design-22.2.1.pom
         https://jcenter.bintray.com/com/android/support/design/22.2.1/design-22.2.1.jar
         https://repo1.maven.org/maven2/com/android/support/design/22.2.1/design-22.2.1.pom
         https://repo1.maven.org/maven2/com/android/support/design/22.2.1/design-22.2.1.jar
         file:/C:/Users/SV_Laptop03/AppData/Local/Android/sdk/extras/android/m2repository/com/android/support/design/22.2.1/design-22.2.1.pom
         file:/C:/Users/SV_Laptop03/AppData/Local/Android/sdk/extras/android/m2repository/com/android/support/design/22.2.1/design-22.2.1.jar
         file:/C:/Users/SV_Laptop03/AppData/Local/Android/sdk/extras/google/m2repository/com/android/support/design/22.2.1/design-22.2.1.pom
         file:/C:/Users/SV_Laptop03/AppData/Local/Android/sdk/extras/google/m2repository/com/android/support/design/22.2.1/design-22.2.1.jar
     Required by:
         MyApp:app:unspecified  
HarryBalls
  • 133
  • 1
  • 1
  • 8

4 Answers4

31

It sounds like you do not have the library included as a Gradle dependency.

Add the following to your build.gradle:

dependencies {
    compile 'com.android.support:design:22.2.1'
}

The error message "Could not find com.android.support:design:22.2.1." means that you likely do not have the latest version of the Android Support Repository installed. Open up your SDK Manager and ensure that the "Android Support Library" and "Android Support Repository" items under "Extras" are up to date.

Bryan Herbst
  • 66,602
  • 10
  • 133
  • 120
  • Thank you for the reply, can you look at my edit? :) – HarryBalls Jul 28 '15 at 16:34
  • @Tanis.7x Hi, how would I know if I needed to add dependencies in the gradle? And also, where do I find that 'com.android.support:design:22.2.1'. Thank you. I'm new to android and still getting used to things like this. :) – christianleroy Jul 20 '16 at 01:03
  • 1
    @keeztian open up your build.gradle file. If it doesn't have that dependency listed, you need to add it. The support library should already be on your machine, but if it isn't Android Studio will prompt you to install it. Make sure you are also using the latest version- 22.2.1 is fairly old at this point (latest is 24.1.0). – Bryan Herbst Jul 20 '16 at 13:42
14

If you're using AndroidX, Use:

implementation 'com.google.android.material:material:1.0.0'

And in you're layout resource file:

<com.google.android.material.navigation.NavigationView
...
...
/>
Javad Arjmandi
  • 331
  • 1
  • 3
  • 12
3

Search for NavigationView in the Android Studio pallet, and click on the Download arrow next to it. It will add the following to your code:

import com.google.android.material.navigation.NavigationView;
Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77
2

For those who are looking for BottomNavigationView and entered this question, you need to have the design packages greater than 25

dependencies {
    compile 'com.android.support:design:25.0.0'
}
Fangming
  • 24,551
  • 6
  • 100
  • 90