thomi
thomi

Reputation: 1687

BottomNavigationView does not highlight custom icon

I want to implement a BottomNavigationView and have added one of material.io's icons as a png to my drawables. When I insert it as:

<?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_dashboard"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:icon="@drawable/ic_dashboard_black_24dp"
    android:title="@string/title_dashboard" />

<item
    android:id="@+id/navigation_notifications"
    android:icon="@drawable/ic_notifications_black_24dp"
    android:title="@string/title_notifications" />

<!-- This is my item added to the normal template -->
<item
    android:id="@+id/navigation_more"
    android:icon="@drawable/ic_more_horiz_black_24dp"
    android:title="@string/title_more" />

</menu>

And use it in the main activity, It shows up, but the item will not highlight when pressed on the simulator, whereas the others will ( by highlight, I mean it will slightly blow up and change to the primary color, showing some text underneath). I tried both vectors and .pngs, nothing will work. I am running backwards compatible to Android 5.0 (Target Version 27).

The home Activity currently looks like:

public class HomeActivity extends AppCompatActivity {

  private TextView mTextMessage;

  private BottomNavigationView.OnNavigationItemSelectedListener 
  mOnNavigationItemSelectedListener
        = new BottomNavigationView.OnNavigationItemSelectedListener() {

  @Override
  public boolean onNavigationItemSelected(@NonNull MenuItem item) {
      switch (item.getItemId()) {
          case R.id.navigation_home:
              mTextMessage.setText(R.string.title_home);
              return true;
          case R.id.navigation_dashboard:
              mTextMessage.setText(R.string.title_dashboard);
              return true;
          case R.id.navigation_notifications:
              mTextMessage.setText(R.string.title_notifications);
              return true;
          case R.id.navigation_more:
              mTextMessage.setText(R.string.title_more);
      }
      return false;
    }
  };

  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_home);

      mTextMessage = (TextView) findViewById(R.id.message);
      BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
}

}

The text view is set correctly, the icon just won't highlight.

Upvotes: 0

Views: 617

Answers (1)

Gal Yedidovich
Gal Yedidovich

Reputation: 787

your navigation method always return false, try return true when there is a case handled by the switch ;)

for example:

public boolean onNavigationItemSelected(@NonNull MenuItem item) {
    switch (item.getItemId()) {
        case R.id.navigation_home:
            mTextMessage.setText(R.string.title_home);
            return true;
        case R.id.navigation_dashboard:
            mTextMessage.setText(R.string.title_dashboard);
            return true;
        case R.id.navigation_notifications:
            mTextMessage.setText(R.string.title_notifications);
            return true;
        case R.id.navigation_more:
            mTextMessage.setText(R.string.title_more);
            return true; // this was my mistake...
        default: 
            return false;
    } 
    return false;
}

Upvotes: 1

Related Questions