0

I am using an android navigation drawer to work with a Floating Action Button. I have coded for that but when the app runs, the navigation drawer opens and closes, but when I click on an Item it doesn't do the task that I want it to do.

<com.google.android.material.floatingactionbutton.FloatingActionButton
    android:id="@+id/btn_menu"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_menu"
    />

The following is the Java code for my Navigation Drawer

    final DrawerLayout drawer = findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, R.string.navigation_drawer_open,R.string.navigation_drawer_close);
    drawer.addDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView = findViewById(R.id.nav_view);
    Menu menu = navigationView.getMenu();

    FloatingActionButton btnMenu = findViewById(R.id.btn_menu);
    btnMenu.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(!drawer.isDrawerOpen(Gravity.LEFT)) drawer.openDrawer(Gravity.LEFT);
            else drawer.closeDrawer(Gravity.RIGHT);
        }
    });

@Override
public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    int id = item.getItemId();
    if (id == R.id.item_1) {
        //Update App

    } else if (id == R.id.item_2) {
        Intent i = new Intent(HomeActivity.this, ProfileActivity.class);
        startActivity(i);
    } else if (id == R.id.item_3) {
        Intent i = new Intent(HomeActivity.this, SubscriptionsActivity.class);
        startActivity(i);
    } else if (id == R.id.item_4) {
        //Share App
    } else if (id == R.id.item_5) {
        Intent i = new Intent(HomeActivity.this, SupportActivity.class);
        startActivity(i);
    } else if (id == R.id.item_6) {
        Intent i = new Intent(HomeActivity.this, AboutActivity.class);
        startActivity(i);
    } else if (id == R.id.item_7) {
        //Privacy Policy
    } else if (id == R.id.item_8) {
        //Our Website
    }

    DrawerLayout drawer = findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}

Also I have implemented implements NavigationView.OnNavigationItemSelectedListener to my class

The following is the drawer layout

<?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"
android:background="@color/bgcolor"
tools:openDrawer="start">

<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/activity_main_drawer" />
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/btn_menu"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="start|top"
        android:layout_margin="10dp"
        app:elevation="2dp"
        app:fabCustomSize="40dp"
        android:src="@drawable/ic_menu"
        />
</RelativeLayout>

</androidx.drawerlayout.widget.DrawerLayout>


0 Answers0