Eugene
Eugene

Reputation: 60184

What is the size of ActionBar in pixels?

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

Upvotes: 284

Views: 248224

Answers (15)

6rchid
6rchid

Reputation: 1292

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>

Upvotes: 0

Reza Mohammadi
Reza Mohammadi

Reputation: 126

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

Upvotes: 3

Md. Enamul Haque
Md. Enamul Haque

Reputation: 1026

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;
}

Upvotes: 1

MrMaffen
MrMaffen

Reputation: 1647

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

Upvotes: 16

Min2
Min2

Reputation: 10971

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;
}

Upvotes: 0

flawedmatrix
flawedmatrix

Reputation: 461

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

Upvotes: 17

AZ13
AZ13

Reputation: 14517

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

Upvotes: 613

Muhammad
Muhammad

Reputation: 425

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.

Upvotes: 0

JesperB
JesperB

Reputation: 4777

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

@dimen/abs__action_bar_default_height

Upvotes: 9

David Boho
David Boho

Reputation: 2716

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);

Upvotes: 48

aaronsnoswell
aaronsnoswell

Reputation: 6251

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

Upvotes: 5

Manfred Moser
Manfred Moser

Reputation: 29912

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.

Upvotes: 21

Jake Wharton
Jake Wharton

Reputation: 76075

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)...

Upvotes: 64

Vikram Bodicherla
Vikram Bodicherla

Reputation: 7123

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

Upvotes: 36

Matt
Matt

Reputation: 5684

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.

Upvotes: 0

Related Questions