The first item in the navigation bar keep highlighting.
When I click other item on the navigation bar, the content will change but the corresponding item will not be highlighted, just keep highlighting the first item.
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
Fragment fragment=null;
switch (item.getItemId()){
case R.id.number:
fragment=new data();
break;
case R.id.graph:
fragment=new graph();
break;
}
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.content_drawer, fragment);
fragmentTransaction.commit();
return true;
}
});
this is the listener
<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"
>
<android.support.design.widget.BottomNavigationView
android:id="@+id/navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimaryDark"
android:layout_alignParentBottom="true"
android:layout_gravity="bottom"
app:menu="@menu/navigation" />
<LinearLayout
android:id="@+id/graphlayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/navigation"
android:orientation="vertical"
android:gravity="center_vertical">
</LinearLayout>
</RelativeLayout>
this is the xml
<?xml version="1.0" encoding="utf-8"?>
<item
android:id="@+id/number"
android:title="number"
android:checkable="true"/>
<item
android:id="@+id/graph"
android:title="graph"
android:checkable="true"/>
</menu>
this is the meun.xml
This usually happens beacause of when onNavigationItemSelected method return false value. You can check this with remove all code except return true inside the onNavigationItemSelected method. Just like this;
bottomNavigationView.setOnNavigationItemSelectedListener(new
BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
return true;
}
});
Then you'll see it works but the content was not changed. For change with content, If I were you I'll change the default return value as false like this;
bottomNavigationView.setOnNavigationItemSelectedListener(new
BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.number:
//Open fragment or do whatever you want.
return true;
case R.id.graph:
//Open another fragment or do whatever you want.
return true;
}
return false;
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With