Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a listener for bottom navigation and navigation drawer in the same activity?

Tags:

java

android

//Here is my java code

public class HomeActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {

    BottomNavigationView bottomNavigationView;
    NavigationView navigationView;

    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener = new BottomNavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull final MenuItem item) {
            switch (item.getItemId()) {
                case R.id.home:
                    HomeFragment homeFragment=new HomeFragment();
                    android.support.v4.app.FragmentTransaction fragmentTransaction=getSupportFragmentManager().beginTransaction();
                    fragmentTransaction.replace(R.id.frameLayout,homeFragment).commit();
                    return true;

                case R.id.navigation_stylist:
                    StylistFragment stylistsFragment=new StylistFragment();
                    android.support.v4.app.FragmentTransaction fragmentTransaction1=getSupportFragmentManager().beginTransaction();
                    fragmentTransaction1.replace(R.id.frameLayout,stylistsFragment).commit();
                    return true;

                case R.id.navigation_apps:
                    MyapptsFragment myaaptsFragment=new MyapptsFragment();
                    android.support.v4.app.FragmentTransaction fragmentTransaction2=getSupportFragmentManager().beginTransaction();
                    fragmentTransaction2.replace(R.id.frameLayout,myaaptsFragment).commit();
                    return true;

                case R.id.navigation_tips:
                    HairtipsFragment hairtipsFragment=new HairtipsFragment();
                    android.support.v4.app.FragmentTransaction fragmentTransaction3=getSupportFragmentManager().beginTransaction();
                    fragmentTransaction3.replace(R.id.frameLayout,hairtipsFragment).commit();
                    return true;

                case R.id.navigation_account:
                    AccountFragment accountFragment=new AccountFragment();
                    android.support.v4.app.FragmentTransaction fragmentTransaction4=getSupportFragmentManager().beginTransaction();
                    fragmentTransaction4.replace(R.id.frameLayout,accountFragment).commit();

                    return true;
            }
            return false;
        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //start onboarding when app is opening first time
        isUserFirstTime = Boolean.valueOf(Utils.readSharedSetting(HomeActivity.this, PREF_USER_FIRST_TIME, "true"));
        Intent introIntent = new Intent(HomeActivity.this, OnboardingActivity.class);
        introIntent.putExtra(PREF_USER_FIRST_TIME, isUserFirstTime);

        if (isUserFirstTime)
            startActivity(introIntent);

        setContentView(R.layout.activity_home);

        bottomNavigationView = findViewById(R.id.bottom_navigation);
        navigationView=findViewById(R.id.nav_drawer);


        //bottom navigationview listener
        bottomNavigationView.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
        BottomNavigationViewHelper.disableShiftMode(bottomNavigationView);

        //navigation drawer listener
        navigationView.setNavigationItemSelectedListener(this);


        //open home fragment on first launch
        HomeFragment homeFragment=new HomeFragment();
        android.support.v4.app.FragmentTransaction fragmentTransaction=getSupportFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.frameLayout,homeFragment).commit();

    }


    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {
            case R.id.nav_home:
                HomeFragment homeFragment=new HomeFragment();
                android.support.v4.app.FragmentTransaction fragmentTransaction=getSupportFragmentManager().beginTransaction();
                fragmentTransaction.replace(R.id.frameLayout,homeFragment).commit();
                return true;

            case R.id.nav_products:
                StylistFragment stylistsFragment=new StylistFragment();
                android.support.v4.app.FragmentTransaction fragmentTransaction1=getSupportFragmentManager().beginTransaction();
                fragmentTransaction1.replace(R.id.frameLayout,stylistsFragment).commit();
                return true;

            case R.id.nav_promotions:
                MyapptsFragment myaaptsFragment=new MyapptsFragment();
                android.support.v4.app.FragmentTransaction fragmentTransaction2=getSupportFragmentManager().beginTransaction();
                fragmentTransaction2.replace(R.id.frameLayout,myaaptsFragment).commit();
                return true;

            case R.id.nav_purchases:
                HairtipsFragment hairtipsFragment=new HairtipsFragment();
                android.support.v4.app.FragmentTransaction fragmentTransaction3=getSupportFragmentManager().beginTransaction();
                fragmentTransaction3.replace(R.id.frameLayout,hairtipsFragment).commit();
                return true;

            case R.id.nav_settings:
                AccountFragment accountFragment=new AccountFragment();
                android.support.v4.app.FragmentTransaction fragmentTransaction4=getSupportFragmentManager().beginTransaction();
                fragmentTransaction4.replace(R.id.frameLayout,accountFragment).commit();
                return true;
        }
        return false;
    }
}

// Here is my xml

<?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:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_drawer"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:theme="@style/menu_text_style"
        app:menu="@menu/navigation_drawer" />

    <!--app:headerLayout="@layout/nav_header_main"-->

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <FrameLayout
            android:id="@+id/frameLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_above="@+id/shadow"
            android:animateLayoutChanges="true">

        </FrameLayout>

        <View
            android:id="@+id/shadow"
            android:layout_width="match_parent"
            android:layout_height="@dimen/_1sdp"
            android:layout_above="@id/bottom_navigation"
            android:background="@color/shadow"/>


        <android.support.design.widget.BottomNavigationView
            android:id="@+id/bottom_navigation"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            app:itemIconTint="@color/navigationitem"
            app:itemTextColor="@color/navigationitem"
            app:menu="@menu/navigation_item"/>
    </RelativeLayout>

</android.support.v4.widget.DrawerLayout>

I want to create both navigation drawer and bottom navigation in the same activity. I created XML design now I have to write java code for both. Bottom navigation is working perfectly but navigation item clicklistener is not working. I created fragments for navigation drawer and also for bottom navigation. When I click the items in navigation drawer it's not redirecting to respective fragment. The given clicklistener for navigation drawer is not at all working.

like image 250
Deepa Dinesh Avatar asked Oct 29 '25 17:10

Deepa Dinesh


1 Answers

Solution:

Try the following steps to get it working:

Step1: Implement onNavigationItemSelected as shown below:

.... extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener { 

Step2: Use the method generated by the interface as:

@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
    switch (item.getItemId()) {
        case R.id.your_drawer_item_id:
            ....
            ....
        case R.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.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"
    tools:openDrawer="start">

    <include
        layout="@layout/app_bar_vendor_drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <android.support.design.widget.NavigationView
        android: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"?>
<RelativeLayout 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"
    tools:context="logixtic.android.web.vd.driver.activities.DriverDrawerActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/MyMaterialTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android: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>

    <FrameLayout
        android: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>

    <View
        android:layout_width="match_parent"
        android:layout_height="3dp"
        android:background="@drawable/toolbar_dropshadow_above"
        android:layout_above="@id/bottom_navigation"/>

    <android.support.design.widget.BottomNavigationView
        android: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.

like image 136
Ümañg ßürmån Avatar answered Oct 31 '25 06:10

Ümañg ßürmån



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!