Solution 1 :
In build.gradle(app) in ‘dependencies’ add
implementation "com.google.android.material:material:1.2.0-alpha02"
Problem :
I am a beginner in android development and I have encountered some difficulty in using the navigation drawer. I am developing an app with a navigation drawer on the left side. But when I add the navigation view to the xml file of the Main activity, it gives this error message:
Class referenced in the layout file, com.google.android.material.navigation.NavigationView, was not found in the project or the libraries
Cannot resolve class com.google.android.material.navigation.NavigationView
This is my code for the Main activity:
package com.hfad.catchat;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Fragment fragment = new InboxFragment();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.add(R.id.content_frame, fragment);
ft.commit();
}
}
This is the xml file of the Main activity:
<?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"
android_id="@+id/drawer_layout"
android_layout_width="match_parent"
android_layout_height="match_parent" >
<LinearLayout
android_layout_width="match_parent"
android_layout_height="match_parent"
android_orientation="vertical" >
<include
layout="@layout/toolbar_main"
android_id="@+id/toolbar" />
<FrameLayout
android_id="@+id/content_frame"
android_layout_width="match_parent"
android_layout_height="match_parent" />
</LinearLayout>
<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"
app_headerLayout="@layout/nav_header"
app_menu="@menu/menu_nav" />
</androidx.drawerlayout.widget.DrawerLayout>
Comments
Comment posted by Gowrishankar
Did you added material dependency in the Gradle file?
Comment posted by Gabriele Mariotti
It is curious the reason why you are suggesting an alpha02 when there is a beta01