6

This code is below it is changing inside of toolbars general title size but I need to change menu item text size. I tried lots of question answer but it doesn't affect directly menu item size. I want to change when item app:showAsAction="ifRoom". Do you have idea?

Menu Item (I need to change this ones size)

    android:id="@+id/try"
    app:showAsAction="ifRoom"
    android:title="0"
    />

Styles Xml

<style name="ActionBar.nameText" parent="TextAppearance.AppCompat.Widget.ActionBar.Title">
        <item name="android:textSize">50sp</item>
        <item name="android:textStyle">bold</item>
    </style>

Toolbar

android.support.v7.widget.Toolbar

        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        android:elevation="4dp"
        android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
android:titleTextAppearance="@style/ActionBar.nameText"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"

        />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        android:id="@+id/header">


        <Button
            android:id="@+id/level_text"
            android:background="@drawable/levelstar"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:text="15"
            android:textSize="15sp"
            android:layout_centerHorizontal="true"/>

        />

    </RelativeLayout>

Image

enter image description here

Doğanay Şahin
  • 91
  • 1
  • 1
  • 7
  • 2
    Possible duplicate of [How to change the toolbar text size?](http://stackoverflow.com/questions/28487312/how-to-change-the-toolbar-text-size) – basic Jul 13 '16 at 14:04
  • Thanks basic but i tried that before but just title size is changing inside of toolbar I need to change menu item text size it does not affect menu item size.. – Doğanay Şahin Jul 13 '16 at 14:12

1 Answers1

7

You have two ways you can do this depending on how exactly you want everything to look/function.

Option 1:

Create a custom style for the entire popup menu. The only drawback with this is it will alter all menu items and not just one.

Create a style in Styles.xml

<style name="MyMenu">
    <item name="android:textSize">17sp</item> //size desired.
    <item name="android:textColor">@color/colorAccent</item> //font color
    <item name="android:background">#FFFFFF</item> //background
</style>

Then apply this style to your toolbar:

<android.support.v7.widget.Toolbar
  android:id="@+id/toolbar"
  android:layout_width="match_parent"
  android:layout_height="?attr/actionBarSize"
  android:background="?attr/colorPrimary"
  app:popupTheme="@style/MyMenu" //Important line
/>

The result becomes:

enter image description here

Option Number 2

You can programmatically set an individual menu item to have a custom font and style. To do that you just need to know the index of whatever menu item you wish to manipulate.

In your onCreateOptionsMenu you will need to add something like:

  @Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    MenuItem item = menu.getItem(0); //here we are getting our menu item.
    SpannableString s = new SpannableString(item.getTitle()); //get text from our menu item.
    s.setSpan(new ForegroundColorSpan(Color.RED), 0, s.length(), 0); //here I am just setting a custom color to the menu item. leave this out if you want it left at black.
    s.setSpan(new RelativeSizeSpan(.7f),0,s.length(),0); //here is where we are actually setting the size with a float (proportion).
    item.setTitle(s); //Then just set the menu item to our SpannableString.

    return true;
}

Now this would look like this:

enter image description here

basic
  • 3,348
  • 3
  • 21
  • 36
  • 1
    Thanks basic for explanation .I tried and yes its working but if I use app:showAsAction="never" . But i tried to use app:showAsAction="ifRoom" .When i tried this one its not working at least it doesn't seem on the screen like that because when i tried toast it was working. – Doğanay Şahin Jul 14 '16 at 12:22
  • Could you add a screenshot of what is happening if you use ifRoom? – basic Jul 14 '16 at 12:43
  • You may need make your theme inherit from a suiatable parent theme. For example: ` – Mr-IDE Apr 16 '21 at 09:35