Akshat Aggarwal
Akshat Aggarwal

Reputation: 89

Android custom toolbar menu item title color and style

I am drawing a custom toolbar which needs to be displayed at the bottom of the screen i.e. it is not the action bar.

Now, I have a toolbar and a custom menu.

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">
    <item 
          android:title="@string/get_directions"
          app:showAsAction="always|withText"
          android:icon="@drawable/navigation_icon/>
</menu>

toolbar layout inside a linear layout.

<android.support.v7.widget.Toolbar
        android:id="@+id/toolbar_options"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"/>

I use toolbar.inflatemenu() to inflate the above menu. Now, I want to customize the appearance (like color, font etc) of the menu item title. Is there any way to do that except using actionlayout in the menuitem?

Please note that its not an "action bar". All the solutions i found on internet are for action bars. Please suggest something for standalone toolbars.

Upvotes: 1

Views: 2199

Answers (2)

afathman
afathman

Reputation: 6143

The styling didn't work for me, so I used:

public static void applyFontForToolbarTitle(Activity context){
    Toolbar toolbar = (Toolbar) context.findViewById(R.id.app_bar);
    for(int i = 0; i < toolbar.getChildCount(); i++){
        View view = toolbar.getChildAt(i);
        if(view instanceof TextView){
            TextView tv = (TextView) view;
            Typeface titleFont = Typeface.
               createFromAsset(context.getAssets(), "fonts/customFont");
            if(tv.getText().equals(context.getTitle())){
                tv.setTypeface(titleFont);
                break;
            }
        }
    }
}

Upvotes: 0

Sagar Devkota
Sagar Devkota

Reputation: 1192

create new style in themes.xml or style.xml

<style name="ToolbarStyle" parent="Theme.AppCompat.Light.DarkActionBar">
         ...
    <item name="actionMenuTextColor">@color/your_color</item>
         ...
 </style>

Now apply style to your toolbar

<android.support.v7.widget.Toolbar
        android:id="@+id/toolbar_options"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:theme="@style/ToolbarStyle"
/>

Upvotes: 2

Related Questions