Is it possible to get a TextView
for the title of a MenuItem
for a NavigationView
? (I want to change the font)
drawer_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<group android:checkableBehavior="single">
<item
android:id="@+id/cards"
android:icon="@drawable/ic_menu_cards"
android:clickable="false"
android:title="Cards"
app:showAsAction="always"/>
<item
android:id="@+id/waiter"
android:icon="@drawable/ic_menu_waiter"
android:clickable="false"
android:title="Timer Mode"
app:showAsAction="always"/>
<item
android:id="@+id/settings"
android:icon="@drawable/ic_menu_settings"
android:title="Settings"
app:showAsAction="always"/>
<item
android:id="@+id/purchase"
android:icon="@drawable/ic_menu_purchase"
android:clickable="false"
android:title="Purchase"
app:showAsAction="always"/>
<item
android:id="@+id/feedback"
android:icon="@drawable/ic_menu_feedback"
android:clickable="false"
android:title="Feedback"
app:showAsAction="always"/>
</group>
</menu>
And my layout:
<android.support.v4.widget.DrawerLayout
android:id="@+id/drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<!-- Removed for simplicity -->
</LinearLayout>
<android.support.design.widget.NavigationView
android:id="@+id/navigation_view"
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:layout_gravity="start"
android:theme="@style/NavigationDrawer"
app:headerLayout="@layout/navigation_view_header"
app:itemTextColor="@color/white"
app:menu="@menu/drawer_menu" />
</android.support.v4.widget.DrawerLayout>
I have seen some other example where casting the MenuItem
to TextView
should work but in my case it just returns null.
TextView title = (TextView) navigationView.getMenu().getItem(0) <-- TextView becomes null
How can I access the title so I can manipulate the font?
More Background info:
I am usually setting the font using DataBindings but for Android Support Library components this does not work so I have to access the TextView manually and set it like this:
textView.setTypeface(FontCache.getInstance(textView.getContext()).get("lato_regular"));
I finally found a way to do it. Its not very pretty but It works.
Pass MenuItems to a method:
setMenuItemFont(binding.navigationView.getMenu().getItem(0));
Method looks like this:
private void setMenuItemFont(MenuItem menuItem) {
SpannableString span = new SpannableString(menuItem.getTitle());
span.setSpan(new CustomTypefaceSpan(FontCache.getInstance(getApplicationContext()).get("lato_regular")), 0 , span.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
menuItem.setTitle(span);
}
Helper class that extends a TypefaceSpan:
package android.app.app;
import android.graphics.Typeface;
import android.text.TextPaint;
import android.text.style.TypefaceSpan;
public class CustomTypefaceSpan extends TypefaceSpan {
private final Typeface newType;
public CustomTypefaceSpan(Typeface type) {
super("");
newType = type;
}
@Override
public void updateDrawState(TextPaint ds) {
ds.setTypeface(newType);
}
@Override
public void updateMeasureState(TextPaint paint) {
paint.setTypeface(newType);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.