Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bottom Navigation View text style on selected item

How do I apply text style on selected item in BottomNavigationView? I can change colours but cannot figure out on how to change font styles (like font family or make it bold/italic) on selected menu item. Is there a way to do this only with XML?

BottomNavigationView

In the sample picture above I want only SEARCH to be in different font and bold. MESSAGE and DASHBOARD remain unchanged.

in activity xml:

<android.support.design.widget.BottomNavigationView
    android:id="@+id/navigation"
    android:layout_width="0dp"
    android:layout_height="49dp"
    android:background="?android:attr/windowBackground"
    android:paddingBottom="5dp"
    android:paddingTop="5dp"
    app:itemIconTint="@color/bottom_nav_color"
    app:itemTextColor="@color/bottom_nav_color"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:menu="@menu/navigation"/>

res/menu/navigation.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:id="@+id/navigation_search"
        android:icon="@drawable/ic_search"
        android:title="SEARCH" />

    <item
        android:id="@+id/navigation_messages"
        android:icon="@drawable/ic_question_answer"
        android:title="MESSAGES" />

    <item
        android:id="@+id/navigation_dashboard"
        android:icon="@drawable/ic_dashboard_black_24dp"
        android:title="DASHBOARD" />
</menu>

res/color/bottom_nav_color.xml

<?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/colorPrimary" />
    <item android:state_checked="false" android:color="#666666"/>
</selector>
like image 547
Mintz Avatar asked Jul 19 '18 20:07

Mintz


2 Answers

There are attributes called itemTextAppearanceActive and itemTextAppearanceInactive in BottomNavigationView. Just create a style in res/values/styles.xml:

<style name="BottomNavigation.ActiveItemTextAppearance" parent="TextAppearance.AppCompat">
    <item name="android:textStyle">bold</item>
    <item name="android:fontFamily">@font/whatever</item>
</style>

Then reference it in BottomNavigationView:

app:itemTextAppearanceActive="@style/BottomNavigation.ActiveItemTextAppearance"

And vice versa for inactive items.

like image 103
degemenc Avatar answered Oct 08 '22 10:10

degemenc


I think it worth to mention here that by changing text style, You won't change text color. Therefore it will still require to use app:itemTextColor

<com.google.android.material.bottomnavigation.BottomNavigationView
...
        app:itemIconTint="@color/bottom_nav_color_selector"
        app:itemTextColor="@color/bottom_nav_color_selector"
        app:itemTextAppearanceInactive="@style/BottomNavigationText"
        app:itemTextAppearanceActive="@style/BottomNavigationText.Selected"
        tools:menu="@menu/menu_bottom_navigation"
        />

bottom_nav_color_selector in res/colors

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

Styles

<style name="BottomNavigationText" parent="@android:style/TextAppearance"/>
<style name="BottomNavigationText.Selected">
    <item name="android:textStyle">bold</item>
</style>
like image 4
murt Avatar answered Oct 08 '22 11:10

murt