I built a NavigationView
using Java code and then I set the text style to keep the text of my menu items in a specific color and font using the method setItemTextAppearance(int resId)
. But this method is not working. My text is not changing.
Note : All the other methods are working great, like setItemTextColor()
, setItemBackground()
etc.
Here is my code:
mNavigationView = new NavigationView(context);
mNavigationView.setId(R.id.nav_view);
mNavigationView.setFitsSystemWindows(true);
// This method works great!
mNavigationView.setBackgroundColor(ContextCompat.getColor(context, R.color.colorPrimary));
// This too.
mNavigationView.setItemTextColor(ColorStateList.valueOf(ContextCompat.getColor(context, android.R.color.white)));
// This method is not working.
mNavigationView.setItemTextAppearance(R.style.MenuTextStyle);
Here is my style code:
<style name="MenuTextStyle">
<item name="android:textSize">16sp</item>
<item name="android:fontFamily">sans-serif-medium</item>
<item name="android:textStyle">normal</item>
<item name="android:textColor">#ffffff</item>
<item name="android:lineSpacingExtra">0sp</item>
</style>
I suppose making your style
a descendant of the default style
that is applied to NavigationView
's items would work out:
<style name="MenuTextStyle" parent="TextAppearance.AppCompat.Medium">
<item name="android:textSize">22sp</item>
<item name="android:textStyle">italic</item>
<item name="android:textColor">#ff0000</item>
</style>
In Java code:
navigationView.setItemTextColor(null);
navigationView.setItemTextAppearance(R.style.MenuTextStyle);
Result