How to get the ActionBar height? How to get the ActionBar height? android android

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 heightTypedValue 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)}


In XML, you should use this attribute:

android:paddingTop="?android:attr/actionBarSize"


Ok I was googling around and get to this post several times so I think it'll be good to be described not only for Sherlock but for appcompat also:

Action Bar height using appCompat

Pretty similiar to @Anthony description you could find it with following code(I've just changed resource id):

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

Pre Sandwitch Problem

I needed action bar height because on pre ICE_CREAM_SANDWITCH devices my view was overlaping action bar. I tried setting android:layout_marginTop="?android:attr/actionBarSize", android:layout_marginTop="?attr/actionBarSize", setting overlap off to view/actionbar and even fixed actionbar height with custom theme but nothing worked. That's how it looked:

overlap problem

Unfortunately I found out that with method described above I get only half of the height(I assume that options bar is not taken in place) so to completely fix the isue I need to double action bar height and everything seems ok:

overlap fixed

If someone knows better solution(than doubling actionBarHeight) I'll be glad to let me know as I come from iOS development and I found most of Android view's stuff pretty confusing :)

Regards,

hris.to