Search code examples
androidandroid-layoutbottomnavigationviewmaterial-componentsmaterial-components-android

Android Studio: Bottom Navigation View - AAPT: error: ' ' is incompatible with attribute drawable (attr) reference


I have added BottomNavigationView in my application like.

main.xml

<com.google.android.material.bottomnavigation.BottomNavigationView
    android:layout_width="match_parent"
    android:layout_height="56dp"
    app:itemBackground="@drawable/nav_bgcolor"
    app:itemIconSize="50dp"
    app:itemIconTint="@color/nav_item_colors"
    app:itemTextColor="@color/nav_item_colors"
    app:labelVisibilityMode="unlabeled"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:menu="@menu/nav_items">
</com.google.android.material.bottomnavigation.BottomNavigationView>

menu/nav_items.xml

<?xml version="1.0" encoding="utf-8"?>

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

    <item
        android:id="@+id/nav_home"
        android:icon="@drawable/home_white"
        android:title="@string/nav_home" />
    <item
        android:id="@+id/nav_market"
        android:icon="@drawable/market_white"
        android:title="@string/nav_market" />
    <item
        android:id="@+id/nav_news"
        android:icon="@drawable/news_white"
        android:title="@string/nav_news" />
    <item
        android:id="@+id/nav_account"
        android:icon="@drawable/account_white"
        android:title="@string/nav_account" />
</menu>

drawable/nav_bgcolor.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="#FF9800" android:state_checked="true" />
    <item android:drawable="#FFFFFF" android:state_checked="false" />
</selector>

color/nav_item_colors.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="#FFFFFF" android:state_checked="true"/>
    <item android:color="#727272"/>
</selector>

this is the preview in the android studio

https://drive.google.com/file/d/1yICxFcTbSxYpILDtxg75Z4x-2Y1UaPM_/view?usp=sharing

it is actually displaying the design that i want to happen.

there is no error or something but when i run the app there are errors in drawable/nav_bgcolor.xml

C:\Users\[PCNAME]\AndroidStudioProjects\[APPNAME]\app\src\main\res\drawable\nav_bgcolor.xml:3: AAPT: error: '#FF9800' is incompatible with attribute drawable (attr) reference.
C:\Users\[PCNAME]\AndroidStudioProjects\[APPNAME]\app\src\main\res\drawable\nav_bgcolor.xml:4: AAPT: error: '#ffffff' is incompatible with attribute drawable (attr) reference.

the error happened after i added app:itemBackground="@drawable/nav_bgcolor" in the main.xml

I have searched the internet but no luck in getting any solution.


Solution

  • In your selector you are using

    <selector>
        <item android:drawable="#FF9800" android:state_checked="true" />
    

    You can't use a color (#FF9800) where you should define an android:drawable.

    Use

    <menu>
        <item
            android:id="@+id/nav_home"
            android:icon="@drawable/icon_add"
            android:title="@string/nav_home" />
        ...
    </menu>
    

    where icon_add is a simple drawable.
    And in your BottomNavigationView:

    <com.google.android.material.bottomnavigation.BottomNavigationView
         app:itemIconTint="@color/nav_item_colors"
         ../>