mikepenz / Android-ActionItemBadge

This library offers a simple method to add a small badge icon to your ActionBar-MenuItem
http://mikepenz.github.io/Android-ActionItemBadge
Apache License 2.0
1.27k stars 220 forks source link

Menu Icon not displaying. #68

Closed SachinBahukhandi closed 6 years ago

SachinBahukhandi commented 6 years ago

Here is my menu_item.xml

<?xml version="1.0" encoding="utf-8"?>
<menu 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"
    tools:context=".MainActivity">

    <item
        android:id="@+id/action_notification"
        android:orderInCategory="300"
        android:title="User"
        app:actionLayout="@layout/menu_action_item_badge"
        android:icon="@drawable/ic_notifications_black_24dp"
        app:showAsAction="always">
    </item>

</menu>

Inside MainActivity.java

MenuItem item= menu.findItem(R.id.action_notification);
            ActionItemBadge.update(item,1);

I am calling this inside this segment:

private BroadcastReceiver fcmReciever = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            //Badger badge= Badger.getInstance();

            Toast.makeText(context,menu.findItem(R.id.action_notification).getTitle().toString(),Toast.LENGTH_LONG).show();
            //menu.findItem(R.id.action_notification).setIcon(R.drawable.camera);

            MenuItem item= menu.findItem(R.id.action_notification);
            ActionItemBadge.update(item,1);
            //MainActivity.this.onCreateOptionsMenu(menu);
            //new ActionItemBadgeAdder().act(((Activity) context)).menu(menu).
            //Toast.makeText(context,"recieved",Toast.LENGTH_LONG).show();
        }
    };

Here is the screenshot: error

mikepenz commented 6 years ago

Is the broadcast receiver happening on the main thread?

SachinBahukhandi commented 6 years ago

I have registered the reciever inside the OnCreate.

mikepenz commented 6 years ago

ensure the onReceive happens on the main thread. Also compare your code with the sample app in which it works.

Perhaps it also breaks with the new androidX or material library

SachinBahukhandi commented 6 years ago

How to ensure that? This is the project's design and appcompat ver: implementation 'com.android.support:appcompat-v7:27.1.1' implementation 'com.android.support:design:27.1.0'

mikepenz commented 6 years ago

please use google. that's one of the standard things on android :)

ok these dependencies should be fine. just compare how it works in the sample app

SachinBahukhandi commented 6 years ago

https://stackoverflow.com/a/20248093/5192105 according to this it works on the main thread.

SachinBahukhandi commented 6 years ago

I suppose your sample app does not have use of the method: ActionItemBadge.update(item,1);

SachinBahukhandi commented 6 years ago

Where is the sample app???

mikepenz commented 6 years ago

https://github.com/mikepenz/Android-ActionItemBadge/blob/develop/app/src/main/java/com/mikepenz/actionitembadge/sample/ToolbarActivity.java

it's part of this repository

SachinBahukhandi commented 6 years ago

Yes I had seen it already. The issue is in menu_item.xml file. Because the icon is not displaying even if I don't set the count in the java file.

mikepenz commented 6 years ago

So it is not a problem of the library, but the main menu you defined? Make sure you supply a proper available icon