3

I use the action bar (origin android, not ABS) and position my views relatively on the root layout (RelativeLayout). To calculate the relative position on the screen I used the getParent().getHeight() / getParent().getWidth() methods of my view. With the normal ActionBar it works fine. I get the real height of the parent (Activity height - Actionbar hight, e.g. 690px on the 800px display). But if I use the splitActionBarWhenNarrow option and the bottom bar is shown I get the same height of the parent (e.g. again 690px on the 800px screen). So some of my programmatically positioned vies are under the bottom action bar.

How can I get the real height of the parent, without the action bars?

Edit:

I read the parent width and height on the onWindowFocusChanged method of the activity.

Ahmad
  • 69,608
  • 17
  • 111
  • 137
WebDucer
  • 1,114
  • 2
  • 16
  • 39

2 Answers2

5

Both the top and bottom action bar are the same height. As far as I know, you can not request the size of the action bar programmatically. This post gives an example of how to set the action bar height with a style so that it is consistent.

To determine whether or not the action bar has been split, you must calculate this manually. As per the design guidelines (near the bottom)

How many actions will fit in the main action bar? Action bar capacity is controlled by the following rules:

  • Action buttons in the main action bar may not occupy more than 50% of the bar's width. Action buttons on bottom action bars can use the entire width.

  • The screen width in density-independent pixels (dp) determine the number of items that will fit in the main action bar:

  • smaller than 360 dp = 2 icons

  • 360-499 dp = 3 icons

  • 500-599 dp = 4 icons

  • 600 dp and larger = 5 icons

Example: table of examples

So you want to do something like this.

DisplayMetrics displayMetrics = new DisplayMetrics();
Display display = getWindowManager().getDefaultDisplay();
display.getMetrics(displayMetrics);

float density  = displayMetrics.density;
// width in dp
float width = (displayMetrics.widthPixels / density);

// I will assume you have 3 action bar icons

if (width < 360) {
    // 2 menu items fit.
    // Calculate action bar height with 2*(actionbar height)
    // to accommodate the bottom action bar.

} else {
    // 3+ menu items fit
    // Calculate action bar height with 1*(actionbar height).
}
Community
  • 1
  • 1
Nick Rempel
  • 3,022
  • 2
  • 23
  • 31
3

And how can I detect wether the action bar is splited or not?

Simple. You use boolean values. By default you'll have a split ActionBar if the screen width is smaller than 400dp. There is already one declared(which is the one the ActionBar uses to determine if it is a handset device or a tablet). If you're targeting Android HC+, then you can access the default ActionBars value: android.R.bool.split_action_bar_is_narrow, if you are using ActionBarSherlock: R.bool.abs_split_action_bar_is_narrow. Found here for the default ActionBar, here your ABS.

Now you can access the boolean value like this:

boolean isActionBarSplitted = getResources().getBoolean(R.bool.split_action_bar);
if(isActionBarSplitted){
      // Parent height - SplitActionBar height
}
else{
     // No Split ActionBar
}

Keep in mind, that android.R.bool.split_action_bar_is_narrow is an internal resource, so you have to get it working like described here.

Community
  • 1
  • 1
Ahmad
  • 69,608
  • 17
  • 111
  • 137