1

I m trying to change the Font style of menu item,so i wanted to set MenuItems font style is in "Century-Gothic".

Below is my code

menu.xml

 <item
        android:id="@+id/option_menu_item_1"
        android:orderInCategory="1"`enter code here`
        android:title="option_menu_item_1"
        app:showAsAction="never" />

    <item
        android:id="@+id/option_menu_item_2"
        android:orderInCategory="2"
        android:title="option_menu_item_2"
        app:showAsAction="never" />

    <item
        android:id="@+id/option_menu_item_3"
        android:orderInCategory="3"
        android:title="option_menu_item_3"
        app:showAsAction="never" />
</menu>

MainActivity.java

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        try {
            ViewConfiguration config = ViewConfiguration.get(this);
            Field menuKeyField = ViewConfiguration.class
                    .getDeclaredField("sHasPermanentMenuKey");
            if (menuKeyField != null) {
                menuKeyField.setAccessible(true);
                menuKeyField.setBoolean(config, false);
            }
        } catch (Exception e) {     
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu, menu);
        return super.onCreateOptionsMenu(menu);

    }
}

color.xml

<resources>
    <color name="colorPrimary">#3F51B5</color>
    <color name="colorPrimaryDark">#303F9F</color>
    <color name="colorAccent">#FF00FF</color>
    <color name="skyBlue">#FF0000</color>
</resources>

styles.xml

<resources xmlns:android="http://schemas.android.com/apk/res/android">

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">

        <item name="android:itemBackground">@color/skyBlue</item>
        <item name="android:textStyle">bold</item>
    </style>

</resources>

I m getting output like as show in below image

OutPut

enter image description here

But I want to change MenuItem title font to (Century-Gothic) using gothic.ttf Assets file typeface.

LaurentY
  • 7,495
  • 3
  • 37
  • 55
Madhushri M
  • 13
  • 1
  • 5

1 Answers1

0

Inside in styles.xml add below code

<item name="android:actionMenuTextAppearance">@style/MyActionBar.MenuTextStyle</item>

so the styles.xml look like this.

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">

    <item name="android:itemBackground">@color/skyBlue</item>
    <item name="android:textStyle">bold</item>
   <item name="android:actionMenuTextAppearance">@style/MyActionBar.MenuTextStyle</item>
</style>

<style name="MyActionBar.MenuTextStyle" parent="Theme.AppCompat">
    <item name="actionMenuTextColor">@color/white</item>
   <item name="android:actionMenuTextColor">@color/white</item>

</style>

and then create style for MyActionBar.MenuTextStyle and change the TextView property as you wish.

Note : You can only use custom fonts via Java code, not through layout XML or styles/themes.

for more detail visit this : Set specific font in a styles.xml

Community
  • 1
  • 1
Harshad Pansuriya
  • 20,189
  • 8
  • 67
  • 95