Reputation: 271
I want to set a custom background image in the android action bar for ICS. I rescale it programmatically before showing it, in order to fit exactly in the actionBar. The problem is that I need to know in advance the height of the actionBar but calling actionBar.getHeight() before it is actually shown, returns 0.
If I just try to include it as the actionBar's background in an xml style, there is no way to rescale it by keeping the aspect ratio. If I try to set the background after the actionBar is shown, I get some weird results with the image floating on top, cut in half.
It seems the only way is to resize it programmaticaly. Is there any way to detect the size of the actionBar before I actually request to show it?
Thank you
Upvotes: 4
Views: 2976
Reputation: 9597
int height = 0;
TypedValue typeValue = new TypedValue();
getTheme().resolveAttribute(android.R.attr.actionBarSize, typeValue, true);
height = TypedValue.complexToDimensionPixelSize(typeValue.data,getResources().getDisplayMetrics());
Upvotes: 3
Reputation: 11230
Why don't you use draw9patch image? DEfault size of the ActionBar is 48dip
So other values you might want to know
<!-- Default height of an action bar. -->
<dimen name="action_bar_default_height">48dip</dimen>
<!-- Vertical padding around action bar icons. -->
<dimen name="action_bar_icon_vertical_padding">8dip</dimen>
<!-- Text size for action bar titles -->
<dimen name="action_bar_title_text_size">18dp</dimen>
<!-- Text size for action bar subtitles -->
<dimen name="action_bar_subtitle_text_size">14dp</dimen>
<!-- Top margin for action bar subtitles -->
<dimen name="action_bar_subtitle_top_margin">-3dp</dimen>
<!-- Bottom margin for action bar subtitles -->
<dimen name="action_bar_subtitle_bottom_margin">5dip</dimen>
Upvotes: 3