Skip to content Skip to sidebar Skip to footer

How To Create A Listener For Bottom Navigation And Navigation Drawer In The Same Activity?

//Here is my java code public class HomeActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener { BottomNavigationView bottomNavigationVi

Solution 1:

Answer :

Try the following steps to get it working:

Step1: Implement onNavigationItemSelected as shown below:

.... extendsAppCompatActivityimplementsNavigationView.OnNavigationItemSelectedListener { 

Step2: Use the method generated by the interface as:

@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
    switch (item.getItemId()) {
        caseR.id.your_drawer_item_id:
            ....
            ....
        caseR.id.your_drawer_item_id:
            ....
            ....
    }
}

Step3: In your class declare a Global variable:

public NavigationView navigationView;

Step4: In your onCreate() initialize the variable as:

navigationView = (NavigationView) findViewById(R.id.your_nav_view);

Finally: Setup the navigationView:

navigationView.setNavigationItemSelectedListener(this);

That's it.

Hope it works.

DrawerLayout xml:

<?xml version="1.0" encoding="utf-8"?><android.support.v4.widget.DrawerLayoutxmlns: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"tools:openDrawer="start"><includelayout="@layout/app_bar_vendor_drawer_layout"android:layout_width="match_parent"android:layout_height="match_parent" /><android.support.design.widget.NavigationViewandroid:id="@+id/nav_drawer_view"android:layout_width="wrap_content"android:layout_height="match_parent"android:layout_gravity="start"android:background="@color/White"android:fitsSystemWindows="true"app:itemBackground="@android:color/white"app:itemIconTint="@color/bottom_color"app:itemTextColor="@color/bottom_color"app:headerLayout="@layout/nav_header_vendor_drawer_layout"app:menu="@menu/vendor_drawer_menu" ></android.support.design.widget.NavigationView></android.support.v4.widget.DrawerLayout>

app_bar_vendor_drawer_layout xml

<?xml version="1.0" encoding="utf-8"?><RelativeLayoutxmlns: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"tools:context="logixtic.android.web.vd.driver.activities.DriverDrawerActivity"><android.support.design.widget.AppBarLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:theme="@style/MyMaterialTheme.AppBarOverlay"><android.support.v7.widget.Toolbarandroid:id="@+id/toolbar"android:layout_width="match_parent"android:layout_height="?attr/actionBarSize"android:background="@color/Project_Standard"app:popupTheme="@style/MyMaterialTheme.PopupOverlay" /></android.support.design.widget.AppBarLayout><FrameLayoutandroid:id="@+id/content_frame"android:layout_width="match_parent"android:layout_height="match_parent"android:layout_marginTop="?attr/actionBarSize"android:layout_above="@+id/bottom_navigation"></FrameLayout><Viewandroid:layout_width="match_parent"android:layout_height="3dp"android:background="@drawable/toolbar_dropshadow_above"android:layout_above="@id/bottom_navigation"/><android.support.design.widget.BottomNavigationViewandroid:id="@+id/bottom_navigation"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_alignParentBottom="true"app:itemBackground="@color/White"app:itemIconTint="@color/bottom_color"app:itemTextColor="@color/bottom_color"app:menu="@menu/vendor_bottom_navigation"/></RelativeLayout>

Try it.

Solution 2:

You can use on the same activity using a method that recive a MenuItem parameter like this:

privatevoidmyClickItem(MenuItem item){
    switch (item.getItemId()) {
        case R.id.bottom_home:
            // DO SOMETHINGbreak;

        case R.id.drawer_main:
            // DO SOMETHINGbreak;
      }
}

Then, call like this:

BottomNavigationViewbottomNavigationView=  findViewById(R.id.bottom_navigation);
bottomNavigationView.setOnNavigationItemSelectedListener(item -> { // using lamda
     myClickItem(item); //call herereturntrue;
});

NavigationViewnavigationView= findViewById(R.id.nv);
navigationView.setNavigationItemSelectedListener(item -> {
    myClickItem(item); // call the same method here

    drawerLayout.closeDrawers(); // bonus: hide navigation after clickreturnfalse;
});

You don't need to modify your xml res, it'll work fine.

Post a Comment for "How To Create A Listener For Bottom Navigation And Navigation Drawer In The Same Activity?"