This question already has an answer here:
- How to disable BottomNavigationView shift mode? 16 answers
i am building an application in which i have a BottomNavigationView. Everything works fine until i get to the Activity.
The Navigation is this:
The problem is that it has this default animation so it pushes the active element everytime higher than the rest.
Another Example:
So my question is how to get rid of this default animation and every item is aligned when i switch between them?
My code:
public class MainActivity extends AppCompatActivity {
private BottomNavigationView bottomNavigationView;
private Fragment fragment;
private FragmentManager fragmentManager;
private FragmentTransaction transaction;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setupBottomBar();
}
private void setupBottomBar() {
bottomNavigationView = (BottomNavigationView)findViewById(R.id.bottomBar);
fragmentManager = getSupportFragmentManager();
fragment = new CardDeckFragment();
transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.activity_main, fragment).commit();
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.action_card_deck:{
Toast.makeText(MainActivity.this, "Card Deck Selected", Toast.LENGTH_SHORT).show();
fragment = new CardDeckFragment();
break;
}
case R.id.action_favorites:{
Toast.makeText(MainActivity.this, "Favorites Selected", Toast.LENGTH_SHORT).show();
fragment = new FavoritesFragment();
break;
}
case R.id.action_favorites_another:{
Toast.makeText(MainActivity.this, "Image Selected", Toast.LENGTH_SHORT).show();
fragment = new ImageFragment();
break;
}
case R.id.action_profile:{
Toast.makeText(MainActivity.this, "Profile Selected", Toast.LENGTH_SHORT).show();
fragment = new ProfileFragment();
break;
}
case R.id.action_menu:{
Toast.makeText(MainActivity.this, "Menu Selected", Toast.LENGTH_SHORT).show();
fragment = new MenuFragment();
break;
}
}
transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.activity_main, fragment).commit();
return true;
}
});
}
}
and my layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.realtimegaming.androidnative.testproject.MainActivity">
<android.support.design.widget.BottomNavigationView
android:id="@+id/bottomBar"
android:layout_alignParentBottom="true"
android:background="@color/brown"
android:layout_gravity="bottom"
android:gravity="bottom"
android:layout_marginTop="?attr/actionBarSize"
app:itemBackground="@color/colorPrimary"
app:menu="@menu/bottom_navigation_main"
app:itemIconTint="@color/white"
android:fitsSystemWindows="true"
android:animateLayoutChanges="false"
android:splitMotionEvents="false"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</RelativeLayout>