0

I'm trying to adjust the size of my ActionBar, and even if there are a lot of posts about that no one seems to work for me. I wonder if it has anything to do with the fact that I just upgraded to API 21. Here's my styles.xml:

<style name="AppTheme" parent="Theme.AppCompat.Light">
    <!-- Main theme colors -->
    <!--   your app branding color for the app bar -->
    <item name="colorPrimary">@color/Black</item>

    <!--   darker variant for the status bar and contextual app bars -->
    <item name="colorPrimaryDark">@color/Black</item>

    <!--   theme UI controls like checkboxes and text fields -->
    <!--   native widgets will now be "tinted" with accent color -->
    <item name="colorAccent">@color/Moccasin</item>

    <!--Action bar style-->
    <item name="android:actionBarStyle">@style/AppTheme.ActionBar</item>
    <item name="actionBarStyle">@style/AppTheme.ActionBar</item>

</style>

<style name="AppTheme.ActionBar" parent="Widget.AppCompat.Light.ActionBar">
    <item name="titleTextStyle">@style/AppTheme.ActionBar.TitleText</item>
    <item name="android:titleTextStyle">@style/AppTheme.ActionBar.TitleText</item>
    <item name="actionBarSize">40dp</item>
    <item name="android:actionBarSize">40dp</item>
    <item name="android:height">40dp</item>
</style>

<style name="AppTheme.ActionBar.TitleText" parent="TextAppearance.AppCompat.Widget.ActionBar.Title">
    <item name="android:textSize">16sp</item>
    <item name="android:textColor">@color/DarkGreen</item>
</style>

I would expect the ActionBar to be smaller, but instead nothing changed in its dimension, while for example the text size and color are set correctly as expected. Any ideas?

EDIT: so I partially solved it by adding

<item name="height">40dp</item>

as suggested in some posts, however now I get something like this: enter image description here as if it was cut to the desired size while being still formatted to the original one. How can I solve this? I tried using the additional attribute android:actionBarSize but with no success. The problem persists if I change the size to 80dp, where I get this: enter image description here

How do I make the drawer icon and the question mark icon be resized and recentered according to the ActionBar size?

splinter123
  • 1,183
  • 1
  • 14
  • 34
  • [http://stackoverflow.com/questions/19157304/unable-to-change-actionbar-size?rq=1](http://stackoverflow.com/questions/19157304/unable-to-change-actionbar-size?rq=1) – M D Mar 14 '15 at 11:43
  • See this post: http://stackoverflow.com/questions/17439683/how-to-change-action-bar-size – Anis BEN NSIR Mar 14 '15 at 11:43
  • 1
    why dont you use custom layout instead of action bar – Amrut Bidri Mar 14 '15 at 11:43
  • How do you expect to fit in 5dp actionbar 16sp text? Have you tried not to add so large text in it? Sometimes the system recalculates the "sizes" and not respects the sizes if other things cannot fit in them! – madlymad Mar 14 '15 at 12:56
  • @madlymad 5dp was just an example, of course in the image it is not 5dp (it's 40dp actually). – splinter123 Mar 14 '15 at 13:06
  • I do understand the usage of extreme values to test something that does not seem to work, but something so small may have no effect for other reasons. So, I suggest to try an extremely large value to see if it has any effect or a different color! This way you can test the code in the style is at least being called an applied to the theme! – madlymad Mar 14 '15 at 13:13
  • @madlymad I'm not using extreme values anymore, it's 40dp I edited the text. The problem remains. – splinter123 Mar 14 '15 at 13:15

1 Answers1

0

adjust the size of my ActionBar

actionBarSize is an attr in android:Theme, but the actually one used to control the height of actionbar is the attr of height. You can check it in themes.xml and styles.xml of framewoks.

     <!-- the following code is taken from  API 21 frameworks. -->

 <!-- themes.xml -->
 <style name="Theme">
    ...
    <item name="actionBarStyle">@style/Widget.ActionBar</item>
    ...
 </style>

 <!-- styles.xml -->
 <style name="Widget.ActionBar">
     <item name="background">@drawable/action_bar_background</item>
     <item name="displayOptions">useLogo|showHome|showTitle</item>
     <item name="divider">@drawable/action_bar_divider</item>

     <!-- actionBarSize is just an attr in frameworks for supportting the default height of 
 actionbar
     <item name="height">?attr/actionBarSize</item>

     <item name="paddingStart">0dip</item>
     <item name="paddingTop">0dip</item>
     <item name="paddingEnd">0dip</item>
     <item name="paddingBottom">0dip</item>
     <item name="titleTextStyle">@style/TextAppearance.Widget.ActionBar.Title</item>
     <item name="subtitleTextStyle">@style/TextAppearance.Widget.ActionBar.Subtitle</item>
     <item name="progressBarStyle">@style/Widget.ProgressBar.Horizontal</item>
     <item name="indeterminateProgressStyle">@style/Widget.ProgressBar.Small</item>
     <item name="homeLayout">@layout/action_bar_home</item>
 </style>

So, there are two way to adjust the actionbar height through style attr:

  • use actionBarSize attr before <item name="android:actionBarStyle">@style/xxx</item>
  • use height attr in style which is referenced by actionBarStyle attr, this will override the above actionBarSize attr if exist

How do I make the drawer icon and the question mark icon be resized and recentered according to the ActionBar size?

As far as I know, there is no official method to adjust the vertical layout parameters of "items" on ActionBar, and the default vertical layout parameter is "align center" for action-title and "align top" for drawer and other action-buttons, as for size you mentioned they are fixed and in conformity with "design specs". If you want to change these layout parameters for some reason, take the following code snippet as reference:

    ViewGroup actionbarView = (ViewGroup) findViewById(R.id.action_bar);
    for (int i = 0; i < actionbarView.getChildCount(); i++) {
        // make toggle drawable center-vertical, you can change whatever layoutparameters as you wish
        if (actionbarView.getChildAt(i) instanceof ImageButton) {
            Toolbar.LayoutParams lp = (Toolbar.LayoutParams) actionbarView.getChildAt(i)
                    .getLayoutParams();
            lp.height = 300;
            lp.gravity = Gravity.CENTER_VERTICAL;
        }

        // make action button center-vertical
        if (actionbarView.getChildAt(i) instanceof LinearLayout) {
            Toolbar.LayoutParams lp = (Toolbar.LayoutParams) actionbarView.getChildAt(i)
                    .getLayoutParams();
            lp.height = 200;
            lp.gravity = Gravity.CENTER_VERTICAL;
        }
    }

So, you can see it is just a common way to set layout parameters of view, but you must first check the view structure of ActionBar, because it may be different depends on the method you set ActionBar, for example extends ActionBarActivityor or extends just Activity when target android3.0 and above only.

Terry Liu
  • 601
  • 5
  • 8
  • that code works for the drawer button, it actually re-centers it, however it doesn't work for the menu button (the question mark in my example). I need this simply because I feel that with API 21 the ActionBar is just too big and steals a lot of space to normal content. I don't understand why it should be so difficult to resize it! – splinter123 Mar 14 '15 at 16:23
  • @splinter123 I add some code for action button(your questiong mark). My understanding about _why there is not simple and official support way to resize the drawer and action-button_ is these elements on actionbar are standard and all conform to the "google design specifications". Hope helped. – Terry Liu Mar 14 '15 at 17:52