284

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

Jonik
  • 80,077
  • 70
  • 264
  • 372
Eugene
  • 59,186
  • 91
  • 226
  • 333

15 Answers15

607

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
Cory Petosky
  • 12,458
  • 3
  • 39
  • 44
AZ13
  • 14,397
  • 5
  • 35
  • 31
  • 9
    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. – rekire Feb 01 '13 at 14:35
  • 1
    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. – Alex Semeniuk Jan 17 '14 at 10:36
  • 4
    @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. – Drew Jul 08 '14 at 09:55
  • 1
    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. – Muhammad Sep 03 '14 at 18:14
  • "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. – Nathaniel Jobs May 20 '16 at 03:23
  • This code example returns 74 on Android 5.1 / Nexus 7. The actual actionbar size in GMail app is 85px (measured by taking a screenshot). – Alexey Yakovenko Jun 29 '16 at 20:34
  • `?attr/actionBarSize` (appcompat) only displayed on android device, but not in android studio... – user25 Mar 18 '18 at 14:16
  • ?android:attr/actionBarSize is what works when you migrate to the AndroidX version of the AppCompat library. – Nantoka Feb 15 '19 at 17:19
  • is it in pixels ? – RonTLV Feb 20 '20 at 15:35
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)...

Jake Wharton
  • 75,598
  • 23
  • 223
  • 230
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);
David Boho
  • 2,646
  • 20
  • 17
35

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

Vikram Bodicherla
  • 7,133
  • 4
  • 28
  • 34
  • 3
    Yes, explained here: http://developer.android.com/guide/topics/ui/actionbar.html#GeneralStyles – Pointer Null Jan 12 '12 at 08:10
  • 3
    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 – Adam Jun 04 '13 at 23:43
  • 9
    ABS has `?attr/actionBarSize` (note the lack of android namespace) which works on all API levels. – Jake Wharton Jul 04 '13 at 03:27
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.

Manfred Moser
  • 29,539
  • 13
  • 92
  • 123
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

RevanthKrishnaKumar V.
  • 1,855
  • 1
  • 21
  • 34
flawedmatrix
  • 461
  • 1
  • 4
  • 8
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

stkent
  • 19,772
  • 14
  • 85
  • 111
MrMaffen
  • 1,647
  • 1
  • 17
  • 22
9

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

@dimen/abs__action_bar_default_height
JesperB
  • 4,625
  • 1
  • 36
  • 40
5

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

aaronsnoswell
  • 6,051
  • 5
  • 47
  • 69
2

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
Reza Mohammadi
  • 106
  • 1
  • 1
  • 6
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;
}
Md. Enamul Haque
  • 926
  • 8
  • 14
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.

Muhammad
  • 425
  • 1
  • 6
  • 14
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;
}
Min2
  • 10,751
  • 2
  • 19
  • 22
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.

Community
  • 1
  • 1
Matt
  • 5,404
  • 3
  • 27
  • 39
  • 2
    Strange - getHeight() results in zero. – Eugene Aug 23 '11 at 18:54
  • 1
    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 Apr 11 '12 at 09:10
  • @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 – Display name Oct 07 '13 at 14:27
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>
6rchid
  • 1,074
  • 14
  • 19