I am new to Android and I have a navigation drawer in my MainActivity. I also use Recycler Views to display a list of products to the MainActivity. How can I get the different items of the navigation drawer to click? I think the problem is in the structure of the activity_main.xml file. Here is the code for my activity_main.xml file :
<?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"
android:id="@+id/drawer_layout"
tools:context=".controller.MainActivity">
<com.google.android.material.navigation.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:headerLayout="@layout/header"
app:menu="@menu/main_menu"
android:id="@+id/nav_view"
android:layout_gravity="start"/>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".controller.MainActivity"
android:orientation="vertical">
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/activity_main_header_special_discount"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:textSize="16dp"
android:textStyle="bold"
android:textColor="@color/blue"
android:gravity="center_horizontal"/>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerViewHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"/>
<TextView
android:id="@+id/activity_main_header_articles"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:textSize="16dp"
android:textStyle="bold"
android:textColor="@color/blue"
android:gravity="center_horizontal"/>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerViewVertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="4dp"/>
</LinearLayout>
</androidx.core.widget.NestedScrollView>
</LinearLayout>
</androidx.drawerlayout.widget.DrawerLayout>
Here is the code for the drawer in my MainActivity.java file :
public class MainActivity extends AppCompatActivity {
DrawerLayout drawerLayout;
NavigationView navigationView;
ActionBarDrawerToggle drawerToggle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
drawerLayout = findViewById(R.id.drawer_layout);
navigationView = findViewById(R.id.nav_view);
drawerToggle = new ActionBarDrawerToggle(MainActivity.this, drawerLayout, R.string.open, R.string.close);
drawerLayout.addDrawerListener(drawerToggle);
drawerToggle.syncState();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId())
{
case R.id.home :
Toast.makeText(MainActivity.this, "Home selected", Toast.LENGTH_LONG).show();
break;
case R.id.createAccount :
Toast.makeText(MainActivity.this, "Create account selected", Toast.LENGTH_LONG).show();
break;
case R.id.login:
Toast.makeText(MainActivity.this, "Log in selected", Toast.LENGTH_LONG).show();
break;
}
return false;
}
});
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
if(drawerToggle.onOptionsItemSelected(item)){
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onBackPressed() {
if(drawerLayout.isDrawerOpen(GravityCompat.START)){
drawerLayout.closeDrawer(GravityCompat.START);
}
else{
super.onBackPressed();
}
}
}
Here is the code for the menu file :
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item android:id="@+id/home"
android:icon="@drawable/ic_baseline_home_24"
android:title="Home"/>
<item android:id="@+id/login"
android:icon="@drawable/ic_baseline_login_24"
android:title="Log in"/>
<item android:id="@+id/createAccount"
android:icon="@drawable/ic_baseline_create_24"
android:title="Create account"/>
</group>
</menu>