Skip to content

Instantly share code, notes, and snippets.

@culjo
Forked from anandwana001/MainActivity.java
Created June 3, 2018 23:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save culjo/897103fb8093c4a3424c84ad21d74e86 to your computer and use it in GitHub Desktop.
Save culjo/897103fb8093c4a3424c84ad21d74e86 to your computer and use it in GitHub Desktop.
Implementing Bottom Navigation Bar in your Android App with more than 3 items
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:theme="@style/ThemeOverlay.AppCompat.Dark">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_scrollFlags="scroll|enterAlways"
app:contentInsetStart="@dimen/fab_margin"/>
</android.support.design.widget.AppBarLayout>
<FrameLayout
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
<android.support.design.widget.BottomNavigationView
android:id="@+id/navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="?android:attr/windowBackground"
app:menu="@menu/navigation" />
</android.support.design.widget.CoordinatorLayout>
public class MainActivity extends AppCompatActivity {
@BindView(R.id.navigation)
BottomNavigationView navigation;
@BindView(R.id.toolbar)
Toolbar toolbar;
private int saveState;
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem item) {
Fragment selectedFragment = null;
switch (item.getItemId()) {
case R.id.navigation_home:
selectedFragment = MapFragment.newInstance();
break;
case R.id.navigation_dashboard:
selectedFragment = EventFragment.newInstance();
break;
case R.id.navigation_notifications:
selectedFragment = UserFragment.newInstance();
break;
case R.id.navigation_world:
selectedFragment = WorldFragment.newInstance();
break;
case R.id.navigation_deal:
selectedFragment = DealFragment.newInstance();
break;
}
BottomNavigationViewHelper.disableShiftMode(navigation);
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.content, selectedFragment);
transaction.commit();
return true;
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
BottomNavigationViewHelper.disableShiftMode(navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
setSupportActionBar(toolbar);
int check = getIntent().getIntExtra("check", R.id.navigation_home);
if (savedInstanceState != null) {
navigation.setSelectedItemId(saveState);
} else {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
switch (check) {
case R.id.navigation_home:
transaction.replace(R.id.content, MapFragment.newInstance());
break;
case R.id.navigation_notifications:
transaction.replace(R.id.content, UserFragment.newInstance());
break;
case R.id.navigation_dashboard:
transaction.replace(R.id.content, EventFragment.newInstance());
break;
case R.id.navigation_world:
transaction.replace(R.id.content, WorldFragment.newInstance());
break;
case R.id.navigation_deal:
transaction.replace(R.id.content, DealFragment.newInstance());
break;
}
transaction.commit();
navigation.setSelectedItemId(check);
}
}
@Override
protected void onResume() {
super.onResume();
navigation.setSelectedItemId(saveState);
}
@Override
public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
super.onSaveInstanceState(outState, outPersistentState);
saveState = navigation.getSelectedItemId();
}
}
-keepclassmembers class android.support.design.internal.BottomNavigationMenuView {
boolean mShiftingMode;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment