Search code examples
androidandroid-layoutkotlinbottomnavigationview

BottomNavigation selected icon not displayed?


I use the bottom navigation view to display my menu. when I run the app it's not displaying selected icon and text see below image.(App uses svg(xml) images files)

And TextSize cutting when its length bigger and tab are more than 3 or 4 See this image for text cutting

enter image description here

Here My Code MainActivity xml file

 <android.support.design.widget.BottomNavigationView
            android:id="@+id/navigation"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginEnd="0dp"
            android:layout_marginStart="0dp"
            app:labelVisibilityMode="labeled"
            android:background="?android:attr/windowBackground"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:menu="@menu/navigation"/>

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

Navigation Menu

<item
        android:id="@+id/navigation_home"
        android:icon="@drawable/ic_home_light"
        android:title="@string/str_home"/>

<item
        android:id="@+id/navigation_medidate"
        android:icon="@drawable/ic_meditation_light"
        android:title="@string/str_meditade"/>

<item
        android:id="@+id/navigation_lessions"
        android:icon="@drawable/ic_lesson_light"
        android:title="@string/str_lessions"/>

<item
        android:id="@+id/navigation_sleep"
        android:visible="false"
        android:icon="@drawable/ic_sleep_light"
        android:title="@string/str_sleep"/>

<item
        android:id="@+id/navigation_cources"
        android:icon="@drawable/ic_course_light"
        android:visible="false"
        android:title="@string/str_courses"/>

Navigation Listener

private val mOnNavigationItemSelectedListener = BottomNavigationView.OnNavigationItemSelectedListener { item ->
        when (item.itemId) {
            R.id.navigation_home -> {
                return@OnNavigationItemSelectedListener true
            }
            R.id.navigation_cources -> {
                return@OnNavigationItemSelectedListener true
            }
            R.id.navigation_lessions -> {
                return@OnNavigationItemSelectedListener true
            }
            R.id.navigation_medidate -> {
                return@OnNavigationItemSelectedListener true
            }
            R.id.navigation_sleep -> {
                return@OnNavigationItemSelectedListener true
            }
        }
        false
    }

Solution

  • Try to add a selector for tint icon selector_navigation.xml. Use app:itemIconTint attribute.

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

    and set to BottomNavigationView

     <android.support.design.widget.BottomNavigationView
                    android:id="@+id/navigation"
                    android:layout_width="match_parent"
                    android:layout_height="?attr/actionBarSize"
                    android:layout_gravity="start"
                    android:elevation="4dp"
                    android:background="@color/white"
                    app:itemTextColor="@color/darker_gray"
                    app:menu="@menu/navigation"
                    app:layout_behavior="BottomNavigationBehavior"
                    app:itemIconTint="@drawable/selector_navigation" />