0

I have implemented a nav drawer without using actionbar or toolbar, but I can't handle the on click events for my nav drawer menu items. On clicking the menu items my drawer simply gets closed but the new activity doesn't start.

my xml layout and java code has been attached.i have used the navigationview within the drawer layout which is the parent layout.

public class Dashboard extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
private DrawerLayout navDrawer;
private RelativeLayout rlNav;
private NavigationView navigationView;
private  ActionBarDrawerToggle drawerToggle;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_dashboard);
//got the navdrawer instance
    navDrawer=(DrawerLayout)findViewById(R.id.drawer_layout);
    rlNav=(RelativeLayout)findViewById(R.id.rl_nav_drawer);
    rlNav.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (navDrawer.isDrawerOpen(GravityCompat.START)) {
                navDrawer.closeDrawer(GravityCompat.START);
            } else {
                navDrawer.openDrawer(GravityCompat.START);
            }
        }
    });
     navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(Dashboard.this);
}
//used this interface to handle my click events
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
    switch (menuItem.getItemId()) {
        case R.id.logout: {
            AlertDialog.Builder builder=new AlertDialog.Builder(Dashboard.this);
            builder.setMessage("Sure want to logout?")
                    .setCancelable(false)
                    .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            Intent intent=new Intent(Dashboard.this,Register.class);
                            startActivity(intent);
                            finish();
                        }
                    }).setNegativeButton("No", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                }
            });
            AlertDialog alert=builder.create();
            alert.show();
            break;
        }
        case R.id.quiz:{
            Toast.makeText(this, "quiz", Toast.LENGTH_SHORT).show();
            break;
        }
    }
    //close navigation drawer
    navDrawer.closeDrawer(GravityCompat.START);
    return true;
}
@Override
public void onBackPressed() {
    if (navDrawer.isDrawerOpen(GravityCompat.START)) {
        navDrawer.closeDrawer(GravityCompat.START);
    } else {
        super.onBackPressed();
    }
}

}

//my xml layout

<?xml version="1.0" encoding="utf-8"?>
<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:layout_width="match_parent"
android:id="@+id/drawer_layout"
android:layout_height="match_parent"
tools:context=".Activity.Dashboard">

//here i used my navigation view

<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="250dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"

    app:menu="@menu/nav_drawer_menu"
    app:itemTextColor="@color/colorAccent"
    android:background="@color/white"
    android:fitsSystemWindows="true"
    app:headerLayout="@layout/nav_header">

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


<RelativeLayout
    android:id="@+id/ll_topBar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <RelativeLayout
        android:id="@+id/rl_nav_drawer"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:background="@color/colorPrimary"
        >


        <RelativeLayout
            android:id="@+id/iv_menu"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_centerVertical="true"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="5dp">

            <ImageView
            android:layout_width="24dp"
            android:layout_height="24dp"
            android:src="@drawable/ic_menu"
            android:layout_centerInParent="true"
            />




        </RelativeLayout>


        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:background="@drawable/edittext_background"
            android:layout_marginRight="60dp"
            android:layout_marginLeft="10dp"
            android:layout_toRightOf="@id/iv_menu"
            android:layout_centerVertical="true"

            >

            <EditText
                android:id="@+id/et_search_school"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:hint="Search"
                android:padding="10dp"
                android:background="@color/white"
                android:textSize="14sp" />

            <ImageView
                android:id="@+id/iv_search"
                android:layout_width="20dp"
                android:layout_height="20dp"
                android:layout_alignParentEnd="true"
                android:layout_centerVertical="true"
                android:layout_marginRight="10dp"
                android:background="@drawable/ic_search"
                android:layout_alignParentRight="true" />




           </RelativeLayout>

        </RelativeLayout>


   </RelativeLayout>

</android.support.v4.widget.DrawerLayout>    
akki_buoy
  • 11
  • 4
  • Please show the xml file where the drawer menu resides. – jmart Aug 19 '19 at 10:23
  • It sounds like an issue with the layout. Please [edit] your question to post `activity_dashboard`. – Mike M. Aug 19 '19 at 10:23
  • You closed the nav drawer but didn't start the activity here `if (navDrawer.isDrawerOpen(GravityCompat.START)) { navDrawer.closeDrawer(GravityCompat.START); } else { navDrawer.openDrawer(GravityCompat.START); }` – Anchit Aug 19 '19 at 10:23
  • @MikeM. i have edited my post and attached the layout – akki_buoy Aug 19 '19 at 10:44
  • @jmart i have edited my post and attached the layout – akki_buoy Aug 19 '19 at 10:45
  • The drawer in a `` must be listed last within it. Move the `` to after the outermost ``. – Mike M. Aug 19 '19 at 10:45
  • @Anchit i have tried commenting out the closeDrawer also,still my interface on Click is not working – akki_buoy Aug 19 '19 at 10:46
  • 1
    @MikeM.yeah . . .got it,i overlooked this solution in other posts.Thanks a lot Man!! its my first question asked here and got so quick solution,Thanks a lot!! god bless u! – akki_buoy Aug 19 '19 at 10:52

0 Answers0