I'm using BadgeDrawable
with BottomNavigationView
. But when I put a badge in an Icon of the bottomNavigationView I get this Exception
java.lang.UnsupportedOperationException: Failed to resolve attribute at index 3: TypedValue{t=0x2/d=0x7f0400db a=6}
at android.content.res.TypedArray.getColorStateList(TypedArray.java:597)
at com.google.android.material.resources.MaterialResources.getColorStateList(MaterialResources.java:76)
at com.google.android.material.resources.TextAppearance.<init>(TextAppearance.java:85)
at com.google.android.material.badge.BadgeDrawable$SavedState.<init>(BadgeDrawable.java:222)
at com.google.android.material.badge.BadgeDrawable.<init>(BadgeDrawable.java:424)
at com.google.android.material.badge.BadgeDrawable.createFromAttributes(BadgeDrawable.java:332)
at com.google.android.material.badge.BadgeDrawable.create(BadgeDrawable.java:300)
at com.google.android.material.bottomnavigation.BottomNavigationMenuView.getOrCreateBadge(BottomNavigationMenuView.java:692)
at com.google.android.material.bottomnavigation.BottomNavigationView.getOrCreateBadge(BottomNavigationView.java:689)
at com.app.UI.MainActivity$1.onNavigationItemSelected(MainActivity.java:129)
at com.google.android.material.bottomnavigation.BottomNavigationView$1.onMenuItemSelected(BottomNavigationView.java:241)
at androidx.appcompat.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:834)
at androidx.appcompat.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:158)
at androidx.appcompat.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:985)
at com.google.android.material.bottomnavigation.BottomNavigationMenuView$1.onClick(BottomNavigationMenuView.java:131)
at android.view.View.performClick(View.java:8160)
at android.view.View.performClickInternal(View.java:8137)
at android.view.View.access$3700(View.java:888)
at android.view.View$PerformClick.run(View.java:30236)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:246)
at android.app.ActivityThread.main(ActivityThread.java:8512)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)
I'm setting the number of badge in the Observe
callback of the LiveData
when request is success .
Here is my View
<com.google.android.material.bottomnavigation.BottomNavigationView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:id="@+id/bottom_navigation"
android:background="@android:color/transparent"
app:menu="@menu/bottom_navigation_menu"
app:itemBackground="@drawable/navigation_background_selector"
app:itemIconTint="@color/text_color_drawer_selector"
android:padding="5dp"
app:labelVisibilityMode="unlabeled"
app:elevation="0dp"
/>
How I set number of the badge
int menuItemId = bottom_navigation.getMenu().getItem(1).getItemId();
BadgeDrawable badge = bottom_navigation.getOrCreateBadge(menuItemId);
badge.setNumber(20);
And my Style
<style name="Theme.app" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Primary brand color. -->
<item name="colorPrimary">@color/purple_500</item>
<item name="colorPrimaryVariant">@color/purple_700</item>
<item name="colorOnPrimary">@color/white</item>
<!-- Secondary brand color. -->
<item name="colorSecondary">@color/teal_200</item>
<item name="colorSecondaryVariant">@color/teal_700</item>
<item name="colorOnSecondary">@color/black</item>
<!-- Status bar color. -->
<item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
<!-- Customize your theme here. -->
<item name="colorAccent">@color/colorAccent</item>
</style>
I solved this Exception by changing my Theme
From :
Theme.AppCompat.Light.NoActionBar
To :
Theme.MaterialComponents.Light.NoActionBar