Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the Height of ActionBar [duplicate]

Tags:

android

I am using Actionbar in my application.
I want the Height of the ActionBar programatically hence I am using :

ActionBar.getHeight(); 


But I am getting 0 value of Height. So How can I get the Height of ActionBar?

like image 812
KDeogharkar Avatar asked Dec 12 '12 05:12

KDeogharkar


2 Answers

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()); 
}
like image 75
KDeogharkar Avatar answered Sep 21 '22 22:09

KDeogharkar


Below code will help too. You may need to change getContext() to this

final TypedArray styledAttributes = getContext().getTheme().obtainStyledAttributes(
                    new int[] { android.R.attr.actionBarSize });
mActionBarSize = (int) styledAttributes.getDimension(0, 0);
styledAttributes.recycle();

The answer is copied from this question What is the size of ActionBar in pixels?

For the newer version of Android you could simply do this:

getActionBar().getHeight()
like image 42
Tim Hong Avatar answered Sep 21 '22 22:09

Tim Hong