I have been implementing a bottom navigation bar into my app, the problem is no matter which activity I am on the dashboard icon is the highlighted one. How do I get it so whichever activity I am on is the highlighted one?
public class Dashboard extends AppCompatActivity
implements View.OnClickListener
{
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener()
{
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item)
{
switch (item.getItemId())
{
case R.id.navigation_request:
Intent r = new Intent(Dashboard.this, Request.class);
startActivity(r);
finish();
break;
case R.id.navigation_settings:
Intent s = new Intent(Dashboard.this, AppSettings.class);
startActivity(s);
finish();
break;
}
return false;
}
};
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dashboard);
BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
}
Here is my XML Menu file
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/navigation_home"
android:icon="@drawable/ic_home_black_24dp"
android:title="@string/title_home" />
<item
android:id="@+id/navigation_request"
android:icon="@drawable/ic_request_icon"
android:title="@string/title_request" />
<item
android:id="@+id/navigation_settings"
android:icon="@drawable/ic_icon_settings"
android:title="@string/title_settings" />
</menu>
Here is how I use the navigation in my activity_dashboard
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<!-- ONLY TEXTVIEWS ARE HERE -->
</FrameLayout>
<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" />
</LinearLayout>
Using many different Activity
instances that all duplicate the BottomNavigationView
and its associated code (like the OnNavigationItemSelectedListener
etc) is not recommended. Instead, you would generally use a single Activity
that hosts the BottomNavigationView
as well as multiple Fragment
instances that are .replace()
d into your content area as the user interacts with the BottomNavigationView
.
That being said, let's see if we can solve your problem.
There are two pieces to this puzzle. The first is simple: you have to indicate which item in your BottomNavigationView
should be selected. This can be achieved by calling setSelectedItemId()
. Add something like this to your onCreate()
method.
navigation.setSelectedItemId(R.id.navigation_settings);
The second is a little more complicated. When you call setSelectedItemId()
, the system is going to behave as though a user had tapped on that item. In other words, your OnNavigationItemSelectedListener
will be triggered.
Looking at your posted listener, I notice that you always return false
. If you check the documentation for onNavigationItemSelected()
, you will find
Returns:
true
to display the item as the selected item andfalse
if the item should not be selected
So the call to setSelectedItemId()
won't work without also changing your listener to return true
.
You could still solve the problem with just a setSelectedItemId()
call if you place that call before your setOnNavigationItemSelectedListener()
call, but that's just masking the problem. It'd be better to fix your listener to return true
in cases where you want the tapped item to appear as selected.