简体   繁体   中英

make toolbar notification icon with badge/count in android 5.0 lollipop

How to add badge/count to notification in android 5.0 toolbar. I tried below code and it is working on 4.4. But when i use toolbar, it gives me NPE on countView.

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.dashboad, menu);

    final View menuWork = menu.findItem(R.id.action_work).getActionView();
    countView = (TextView) menuWork.findViewById(R.id.actionbar_notification_textview);

    ImageButton notifyLayout = (ImageButton) menuWork.findViewById(R.id.img);
    menuWork.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    });

    return true;
}

xml code for menu item

<item
    android:id="@+id/action_work"
    android:actionLayout="@layout/notification_alert"
    android:icon="@android:drawable/ic_popup_reminder"
    android:orderInCategory="100"
    android:title="Job"
    app:showAsAction="always" />

Just replace:

android:actionLayout="@layout/notification_alert"

With:

 app:actionLayout="@layout/notification_alert"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM