-1

I'm having an issue where no matter what I try getActionBar().getHeight() always returns 0. the action bar is clearly visible , so I don't know what might be causing the issue.

System.out.println(lP.height + " + " + getActionBar().getHeight());
lP.height = lP.height + getActionBar().getHeight();
System.out.println(lP.height);

there really isn't much code to show. the output looks like:

900 + 0
900

I have all of the app wrapped in a FrameLayout, including the action bar, so that I can place items on top of it, but I know I can still get the right action bar reference using getActionBar() because I can see the background changing when I use getActionBar().setBackgroundDrawable(mDrawable). just for some reason getHeight() always returns 0.

Shef
  • 1,044
  • 1
  • 12
  • 19
  • where do you make the call? – waqaslam Jul 28 '14 at 22:20
  • `onCreate()`. Let me try in other places too to see if it's that. – Shef Jul 28 '14 at 22:21
  • 1
    Try this: http://stackoverflow.com/questions/12301510/how-to-get-the-actionbar-height – Sophicles Jul 28 '14 at 22:22
  • 1
    hmm it seems to work when called from an `OnScrollChangedListener` so it probably has to do with the order in which the view is created. This means I need to call it after onCreate(), because that is the last call in `onCreate()`, so it means the ActionBar is created after the activity is created – Shef Jul 28 '14 at 22:24
  • @Sophicles your answer works, thank you very much. I started looking for an `onDraw` or something similar but this is much nicer. If you want you can write it as a top level answer so I can approve it. – Shef Jul 28 '14 at 22:27
  • height is not yet calculated in **onCreate** method. So, if you try your code in **onResume** then I think it should work. – waqaslam Jul 28 '14 at 22:37
  • See the quesiton here: http://stackoverflow.com/questions/12301510/how-to-get-the-actionbar-height – Sophicles Jul 28 '14 at 22:34

1 Answers1

1

using this Listener GlobalLayoutListener Example

This Listener will be called when its Attached to the Layout with the Correct width and height

just dont forget to remove the listener after your get variables

Community
  • 1
  • 1
Itzik Samara
  • 2,278
  • 1
  • 14
  • 18