How to add bottom navigator into any android application

Acoustic World
Nov 5 · 2 min read

1. create new menu resource folder and add ‘bottomnavigatemodel.xml’

<?xml version="1.0" encoding="utf-8"?><menu xmlns:android="http://schemas.android.com/apk/res/android"    >    <item        android:id="@+id/ic_arrow"        android:icon="@drawable/ic_assignment_black_24dp"        android:title="Summary"        />    <item        android:id="@+id/ic_android"        android:icon="@drawable/ic_history_black_24dp"        android:title="History"        />
<item android:id="@+id/ic_books" android:icon="@drawable/ic_settings_black_24dp" android:title="Settings" />
<item android:id="@+id/ic_post" android:icon="@drawable/ic_insert_chart_black_24dp" android:title="Progress" /></menu>

2.create new java file to bottomnavigate helper, and add these code

import android.annotation.SuppressLint;
import android.support.design.internal.BottomNavigationItemView;
import android.support.design.internal.BottomNavigationMenuView;
import android.support.design.widget.BottomNavigationView;
import android.util.Log;
import java.lang.reflect.Field;public class BottomNavigationViewHelper {
@SuppressLint("RestrictedApi")
public static void disableShiftMode(BottomNavigationView view) {
BottomNavigationMenuView menuView = (BottomNavigationMenuView) view.getChildAt(0);
try {
Field shiftingMode = menuView.getClass().getDeclaredField("mShiftingMode");
shiftingMode.setAccessible(true);
shiftingMode.setBoolean(menuView, false);
shiftingMode.setAccessible(false);
for (int i = 0; i < menuView.getChildCount(); i++) {
BottomNavigationItemView item = (BottomNavigationItemView) menuView.getChildAt(i);
//noinspection RestrictedApi item.setShifting(false);
// set once again checked value, so view will be updated //noinspection RestrictedApi item.setChecked(item.getItemData().isChecked());
}
} catch (NoSuchFieldException e) {
Log.e("BNVHelper", "Unable to get shift mode field", e);
} catch (IllegalAccessException e) {
Log.e("BNVHelper", "Unable to change value of shift mode", e);
}
}
}

3. in your .xml file

<RelativeLayout
android:layout_width="match_parent" android:layout_height="50dp" android:id="@+id/bottomBar" android:layout_alignParentBottom="true">
<android.support.design.widget.BottomNavigationView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/bottomNavView_Bar" android:background="@drawable/white_grey_border_top" app:menu="@menu/bottom_navigation_menu">
</android.support.design.widget.BottomNavigationView>
</RelativeLayout>

4.then in your activity add this code

BottomNavigationView bottomNavigationView =  findViewById(R.id.bottomNavView_Bar);
BottomNavigationViewHelper.disableShiftMode(bottomNavigationView);
Menu menu = bottomNavigationView.getMenu();
MenuItem menuItem = menu.getItem(1);
menuItem.setChecked(true);
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.ic_arrow:
This is an example codeIntent intent1 = new Intent(RecordListActivity.this, MainPageActivity.class);
startActivity(intent1);
break; case R.id.ic_android:
break;
case R.id.ic_books:
break;
case R.id.ic_post:

break;
} return false;
}
});

5. Use this code fragment in all the activity pages to select the relevant image in the bottom navigation.

Menu menu = bottomNavigationView.getMenu();
MenuItem menuItem = menu.getItem(1);
menuItem.setChecked(true);

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade