14

I am using the new Android BottomNavigationView but the bottom navigation MenuItem action view is not showing.

After debugging I found that the menu item is not null and the visibility is visible.

After checking height and width of the root view it's coming 0 even after hardcoding the values in layout layout_width=50dp and layout_height=50dp of the root element.

Here is my bottom navigation menu:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<item
    android:id="@+id/nearby_fragment"
    android:enabled="true"
    android:icon="@drawable/homenearbyfragment"
    android:title="@string/bottom_nearby"
    app:showAsAction="always"/>

<item
    android:id="@+id/route_fragment"
    android:title="@string/bottom_homescreen"
    android:enabled="true"
    app:showAsAction="always"
    android:icon="@drawable/home_mycommute" />

<item
    android:id="@+id/newsfeed_activity"
    android:title="@string/bottom_news"
    android:enabled="true"
    app:showAsAction="always"
    android:icon="@drawable/newsfeed"
    app:actionLayout="@layout/bagde_layout"/>

</menu>

My action layout:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
style="?android:attr/actionButtonStyle"
android:layout_width="50dp"
android:layout_height="50dp"
android:clickable="true"
android:focusable="true"
android:gravity="center">

<ImageView
    android:id="@+id/menu_badge_icon"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/newsfeed" />

<TextView
    android:id="@+id/menu_badge"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="top|end"
    android:layout_marginTop="4dp"
    android:background="@drawable/circle_bg"
    android:gravity="center"
    android:textColor="@android:color/white"
    android:textSize="8sp"
    />

In my Activity I am trying to set:

private void setCountOnNews(Menu menu) {
    mReportMenu = menu.findItem(R.id.newsfeed_activity);
    FrameLayout count = (FrameLayout) mReportMenu.getActionView();
    count.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(MainActivity.this, NewsFeedActivity.class);
            startActivity(intent);
            MainApplication.getInstance().trackScreenView(getString(R.string.tracknewsfeedmenu));
        }
    });
    TextView notifCount = (TextView) count.findViewById(R.id.menu_badge);
    //if (Utils.getNewsfeedCountPreference(MainActivity.this) > 0)
    notifCount.setText(String.valueOf(Utils.getNewsfeedCountPreference(MainActivity.this)));
    notifCount.setText("asd;jfnapsdifnaspdifnasdpifnaspdfnapsdifnaspidfnapsidfnaspdiufif");
    Log.d(TAG, "setCountOnNews:" + notifCount.getText().toString());
    //else
    //  notifCount.setVisibility(View.GONE);
}
Jonathan
  • 6,507
  • 5
  • 37
  • 47
user007
  • 421
  • 1
  • 4
  • 7

2 Answers2

0

Did you forget to add that in your MainActivity?

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}
Android Admirer
  • 2,310
  • 2
  • 17
  • 38
-1

if you are using fragment than you need to set has option menu true.

setHasOptionsMenu(true);
Harshal Shah
  • 427
  • 3
  • 5