How to get the ActionBar height?
@Anthony answer works for devices that support ActionBar
and for those devices which support only Sherlock Action Bar
following method must be used
TypedValue tv = new TypedValue();
if (getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true))
actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data,getResources().getDisplayMetrics());
if(actionBarHeight ==0 && getTheme().resolveAttribute(com.actionbarsherlock.R.attr.actionBarSize, tv, true)){
actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data,getResources().getDisplayMetrics());
}
//OR as stated by @Marina.Eariel
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 if(getTheme().resolveAttribute(com.actionbarsherlock.R.attr.actionBarSize, tv, true){
actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data,getResources().getDisplayMetrics());
}
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)
}
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:
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:
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
In XML, you should use this attribute:
android:paddingTop="?android:attr/actionBarSize"