13

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?

KDeogharkar
  • 10,939
  • 7
  • 51
  • 95

3 Answers3

19

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()); 
}
KDeogharkar
  • 10,939
  • 7
  • 51
  • 95
  • "android.R.attr.actionBarSize" is not working in android version 2.3, but "R.attr.actionBarSize" is working android all version. just use "R.attr.actionBarSize" instead of "android.R.attr.actionBarSize" and etc. – Nathaniel Jobs May 20 '16 at 03:23
  • Anyone know how to it in java (javafx)? – Lealo Jul 26 '17 at 13:01
5

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()
Community
  • 1
  • 1
Tim Hong
  • 2,734
  • 20
  • 23
3

A simple way to find actionbar height is from Activity onPrepareOptionMenu method.

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
           ....
           int actionBarHeight = getActionBar().getHeight());
           .....
    }
Ashish Jha
  • 121
  • 1
  • 5