How to Get the Actionbar Height

How to get the ActionBar height?

While @birdy's answer is an option if you want to explicitly control the ActionBar size, there is a way to pull it up without locking the size that I found in support documentation. It's a little awkward but it's worked for me. You'll need a context, this example would be valid in an Activity.

// Calculate ActionBar height
TypedValue tv = new TypedValue();
if (getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true))
{
int actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data,getResources().getDisplayMetrics());
}

Kotlin:

val tv = TypedValue()
if (requireActivity().theme.resolveAttribute(android.R.attr.actionBarSize, tv, true)) {
val actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data, resources.displayMetrics)
}

How to get actionBar size properly programatically?

You can get Action bar height by

public void changeRecyclerViewPosition() {
TypedValue tv = new TypedValue();
int actionBarHeight = 0;
if (getActivity().getTheme().resolveAttribute(R.attr.actionBarSize, tv, true))
{
actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data,getResources().getDisplayMetrics());
}
params.setMargins(0,actionBarHeight, 0, 0);
myrv.setLayoutParams(params);
}

Get the Height of ActionBar

I found the alternative of ActionBar.getHeight()
If anyone facing the same issue then this link is useful.

While @birdy's answer is an option if you want to explicitly control
the ActionBar size there is a way to pull it up without locking the
size that I found in support documentation. It's a little awkward but
it's worked for me. You'll need a context, this example would be valid
in an Activity. ~ Anthony

// Calculate ActionBar's height 
TypedValue tv = new TypedValue();
if (getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true)) {
actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data,getResources().getDisplayMetrics());
}

get actionbar height in android

You can have two different height between these two methods because they are returning the value of two different things. The first method returns the actual ActionBar height. The second method returns the value of the android attribute actionBarSize.

The correct method to have the real ActionBar height is the first but if you say that you don't want to wait the creation of the views, if you are using Toolbar you can set via xml his height to ?android:attr/actionBarSize and use the second method wherever you want to get the height in pixels.

What is the size of ActionBar in pixels?

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

What's the height of the Android Toolbar?

The Toolbar (if not extended) has the height of:

  • 56dp (default)
  • 48dp (landscape)
  • 64dp (sw600dp; i.e. tablet)

The Toolbar is higher than the pre-lollipop ActionBar; it was 48dp by default, 40dp in landscape and 56dp in sw600dp.

And to retrieve the height of the Toolbar in XML, just use

?android:attr/actionBarSize

or if you're using the AppCompat library this

?attr/actionBarSize

Get navigation/action bar height

App.cs (Common code)

public static int ActionBarHeight;

MainActivity, in the OnCreate method (Droid part):

Android.Content.Res.TypedArray styledAttributes = Theme.ObtainStyledAttributes(new int[] { Android.Resource.Attribute.ActionBarSize });
App.ActionBarHeight = (int)(styledAttributes.GetDimension(0, 0) / Resources.DisplayMetrics.Density);
styledAttributes.Recycle();

If you want the height in pixels:

App.ActionBarHeight = (int)styledAttributes.GetDimension(0, 0);


Related Topics



Leave a reply



Submit