285

I need to know the exact size of ActionBar in pixels so to apply correct background image.

0

15 Answers 15

615

To retrieve the height of the ActionBar in XML, just use

?android:attr/actionBarSize 

or if you're an ActionBarSherlock or AppCompat user, use this

?attr/actionBarSize 

If you need this value at runtime, use this

final TypedArray styledAttributes = getContext().getTheme().obtainStyledAttributes( new int[] { android.R.attr.actionBarSize }); mActionBarSize = (int) styledAttributes.getDimension(0, 0); styledAttributes.recycle(); 

If you need to understand where this is defined:

  1. The attribute name itself is defined in the platform's /res/values/attrs.xml
  2. The platform's themes.xml picks this attribute and assigns a value to it.
  3. The value assigned in step 2 depends on different device sizes, which are defined in various dimens.xml files in the platform, ie. core/res/res/values-sw600dp/dimens.xml
Sign up to request clarification or add additional context in comments.

9 Comments

Great answer. I know the way to dig this informations, but to search this answer was much faster +1. Your answer is also a great hint how to search such things.
Thank you. I was trying to use @dimen/abc_action_bar_default_height directly (ActionBarComapt) and it worked (on mdpi device). But trying to get this value on Samsung Galaxy SIII returned me wrong value. That is because values-xlarge (somehow) is more preferred than values-land when in landscape mode. Referring to the attribute instead works like a charm.
@AZ13 I would like to add that android.R.attr.actionBarSize will resolve to 0 size on pre-3.0 devices. So when using ActionBarCompat one would stick to android.support.v7.appcompat.R.attr.actionBarSize instead.
The guy asked it in pixels. I suppose actionBarSize returns the dp value. I got the value 48dp. That means converting it to pixels gives me 96 pixels for xhdpi.
"android.R.attr.actionBarSize" is not working in android version 2.3, but "R.attr.actionBarSize" is working android all version. just use "R.attr.actionBarSize" instead of "android.R.attr.actionBarSize" and etc.
|
64

From the de-compiled sources of Android 3.2's framework-res.apk, res/values/styles.xml contains:

<style name="Theme.Holo"> <!-- ... --> <item name="actionBarSize">56.0dip</item> <!-- ... --> </style> 

3.0 and 3.1 seem to be the same (at least from AOSP)...

3 Comments

What about the different height of the action bar in landscape?
on 4.0+: default is 48dp, landscape is 40dp, sw600dp is 56dp
Which is why you should use the android:attr value and not a hardcoded value!
48

To get the actual height of the Actionbar, you have to resolve the attribute actionBarSize at runtime.

TypedValue tv = new TypedValue(); context.getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true); int actionBarHeight = getResources().getDimensionPixelSize(tv.resourceId); 

Comments

36

One of the Honeycomb samples refers to ?android:attr/actionBarSize

3 Comments

This is the answer if your min sdk is >=11, if you're min is <11, then most likely you're using ABS, then this is the solution: @dimen/abs__action_bar_default_height
ABS has ?attr/actionBarSize (note the lack of android namespace) which works on all API levels.
21

I needed to do replicate these heights properly in a pre-ICS compatibility app and dug into the framework core source. Both answers above are sort of correct.

It basically boils down to using qualifiers. The height is defined by the dimension "action_bar_default_height"

It is defined to 48dip for default. But for -land it is 40dip and for sw600dp it is 56dip.

Comments

17

If you're using the compatibility ActionBar from the recent v7 appcompat support package, you can get the height using

@dimen/abc_action_bar_default_height 

Documentation

Comments

16

With the new v7 support library (21.0.0) the name in R.dimen has changed to @dimen/abc_action_bar_default_height_material.

When upgrading from a previous version of the support lib you should therefore use that value as the actionbar's height

1 Comment

Perfect! This sure beats ?attr/actionBarSize if one is looking to match a regular ActionBar.
9

If you are using ActionBarSherlock, you can get the height with

@dimen/abs__action_bar_default_height 

3 Comments

Don't use abs__-prefixed resources directly.
@JakeWharton please tell us how to do it correctly instead, then.
Create your own dimension so you are not dependent on abs_.
5

@AZ13's answer is good, but as per the Android design guidelines, the ActionBar should be at least 48dp high.

Comments

3

Accepted answer in Kotlin :

val Context.actionBarSize get() = theme.obtainStyledAttributes(intArrayOf(android.R.attr.actionBarSize)) .let { attrs -> attrs.getDimension(0, 0F).toInt().also { attrs.recycle() } } 

Usage :

val size = actionBarSize // Inside Activity val size = requireContext().actionBarSize // Inside Fragment val size = anyView.context.actionBarSize // Inside RecyclerView ViewHolder 

Comments

1
public int getActionBarHeight() { int actionBarHeight = 0; TypedValue tv = new TypedValue(); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { if (getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true)) actionBarHeight = TypedValue.complexToDimensionPixelSize( tv.data, getResources().getDisplayMetrics()); } else { actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data, getResources().getDisplayMetrics()); } return actionBarHeight; } 

Comments

0

The Class Summary is usually a good place to start. I think the getHeight() method should suffice.

EDIT:

If you need the width, it should be the width of the screen (right?) and that can be gathered like this.

3 Comments

Strange - getHeight() results in zero.
in my development, I noticed that it returns zero in onResume(). Hovever calling getActionBar().getHeight() when you actually need it returns a positive >0 value.
@tos the spec for the getHeight() says that it returns the current width for the ActionBar, which is probably 0 when the view is created
0

On my Galaxy S4 having > 441dpi > 1080 x 1920 > Getting Actionbar height with getResources().getDimensionPixelSize I got 144 pixels.

Using formula px = dp x (dpi/160), I was using 441dpi, whereas my device lies
in the category 480dpi. so putting that confirms the result.

Comments

0

I did in this way for myself, this helper method should come in handy for someone:

private static final int[] RES_IDS_ACTION_BAR_SIZE = {R.attr.actionBarSize}; /** * Calculates the Action Bar height in pixels. */ public static int calculateActionBarSize(Context context) { if (context == null) { return 0; } Resources.Theme curTheme = context.getTheme(); if (curTheme == null) { return 0; } TypedArray att = curTheme.obtainStyledAttributes(RES_IDS_ACTION_BAR_SIZE); if (att == null) { return 0; } float size = att.getDimension(0, 0); att.recycle(); return (int) size; } 

Comments

0

Source: appcompact-1.6.1/res/values

<item name="actionBarSize">@dimen/abc_action_bar_default_height_material</item> // values.xml <dimen name="abc_action_bar_default_height_material">56dp</dimen> //values-land.xml <dimen name="abc_action_bar_default_height_material">48dp</dimen> //values-sw600dp-v13.xml <dimen name="abc_action_bar_default_height_material">64dp</dimen> 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.