1

I'm using Actionbarsherlock and I want to place a PopupWindow right below the action bar. Using the showAtLocation() takes an x and y offset, so ideally the y offset would be the height of the action bar. But when I call

int abHeight = getSupportActionBar().getHeight();

it returns zero. I'm using a SherlockFragmentActivity

Here's the relevant code:

slidingLayout = inflater.inflate(R.layout.sliding_menu, null);
menuDrawer = MenuDrawer.attach(this, MenuDrawer.MENU_DRAG_CONTENT, Position.LEFT);
menuDrawer.setContentView(R.layout.activity_main);
menuDrawer.setMenuView(slidingLayout.findViewById(R.id.sliding_menu));

getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
int abHeight = getSupportActionBar().getHeight();

I've looked all over and can't find a similar question/answer, so has anyone experienced this before? Thanks.

EDIT: Jake's answer was right on. In order to get that attribute value I used this post.

Community
  • 1
  • 1
Wenger
  • 989
  • 2
  • 12
  • 35

3 Answers3

2

You can read the height of the action bar from the actionBarSize theme attribute. This changes based on the device configuration so make sure you are always reading it when your activity is created or recreated.

Jake Wharton
  • 75,598
  • 23
  • 223
  • 230
  • 1
    Thanks Jake. I used http://stackoverflow.com/a/13216807/1754999 to get the size and it all worked. I think there might be a dp to px issue in there somewhere but I'll get that figured out. – Wenger Mar 04 '13 at 02:19
2

in you style.XML add: <item name="@android:attr/actionBarSize">50px</item>

and then in your activity add the following code :

 TypedArray actionbarSizeTypedArray = mContext.obtainStyledAttributes(new int[] {  android.R.attr.actionBarSize});  

        int h = (int) actionbarSizeTypedArray.getDimension(0, 0);  

this is one kind ,I am trying to get other ways.Good luck!

Yeah!I find a way very simple:

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

more info,look this link

Community
  • 1
  • 1
AlexChu
  • 31
  • 4
0

You can't get the height for views until they have been layed out. Try adding a ViewTreeObserver:

someView.getViewTreeObserver().addGlobalLayoutListener(new OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
        // Remember to remove it if you don't want it to fire every time
        someView.getViewTreeObserver().removeGlobalOnLayoutListener(this);

        int abHeight = getSupportActionBar().getHeight();
        // Use the height as desired...
    }
});

Refer to the docs starting at View.getViewTreeObserver().

Jason Sankey
  • 2,328
  • 1
  • 15
  • 12