BottomNavigationView



/** Add the dependency into the gradle file **/
implementation 'com.android.support:design:27.1.1'
implementation 'com.android.support:support-vector-drawable:27.1.1'
implementation 'com.android.support:support-v4:27.1.1'

/** Add this view into xml file **/
 <android.support.design.widget.BottomNavigationView
            android:id="@+id/navigation"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_gravity="bottom"
            android:background="?android:attr/windowBackground"
            android:foreground="?attr/selectableItemBackground"
            app:itemBackground="@color/bgBottomNavigation"
            app:itemIconTint="@android:color/white"
            app:itemTextColor="@android:color/white"

            app:menu="@menu/navigation" />

/** create a folder "menu" inside "res" folder, then inside "menu" folder create navigation layout**/
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/navigation_shop"
        android:icon="@drawable/ic_store_white_24dp"
        android:title="@string/title_special" />
    <item
        android:id="@+id/navigation_event"
        android:icon="@drawable/ic_offers"
        android:title="Offers" />
    <item
        android:id="@+id/navigation_menu"
        android:icon="@drawable/restaurant_menu"
        android:title="Menu" />
    <item
        android:id="@+id/navigation_cart"
        android:icon="@drawable/ic_shopping_cart_white_24dp"
        android:title="@string/title_cart" />
    <item
        android:id="@+id/navigation_profile"
        android:icon="@drawable/ic_person_white_24dp"
        android:title="@string/title_profile" />

</menu>


/** Added this into onCreate activity **/
BottomNavigationView navigation;
navigation = findViewById(R.id.navigation);

navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);

/** Added this outside onCreate function **/
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            Fragment fragment;
            switch (item.getItemId()) {
                case R.id.navigation_shop:
                    return true;
                case R.id.navigation_event:
                    return true;
                case R.id.navigation_menu:
                    return true;
                case R.id.navigation_cart:
                    return true;
                case R.id.navigation_profile:                   
                    return true;
            }
            return false;
        }
    };

Comments