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.
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:
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).
}
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.
Related
I want to create an image in photoshope for actionbar image background, so I have to create new image but I just don't know how to specify its height and width, so that it will be fit in actionbar
any suggestion please
The ActionBar's width vary from every devices, the suggested height of the actionbar is 56dp according to Google Design.
For more detail: http://www.google.com/design/spec/layout/metrics-keylines.html#metrics-keylines-keylines-spacing
Wish it could help.
I want to know which default size an MenuItem with an icon (drawable) has.
I need the percentage in relation to the actionbar width (screen width).
A single action bar icon is 32x32dp.
As a percentage of the screen, it's impossible to say. Different devices have different screen widths.
I need to find a way to actually get the dimensions of the screen of my device. I've been trying for long time now and there I cannot find a way to find the height and width in pixel of the drawable screen.
Here is the code I'm using right now:
SCREEN_HEIGHT = metrics.heightPixels - unusable_height;
Where unusable_height is found by:
//Calculate action Bar Height
if (getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true)){
unusable_height = TypedValue.complexToDimensionPixelSize(tv.data,getResources().getDisplayMetrics());
}
//Calculate the navigation bar height
int resourceId = getResources().getIdentifier("navigation_bar_height", "dimen", "android");
if (resourceId > 0) {
unusable_height = unusable_height + getResources().getDimensionPixelSize(resourceId);
}
This works pefectly in my tablet however in my phone the resulting screen height is too large as stuff gets drawn off the screen. (And I calculate all my sizes according to the screen height). This is in landscape mode.
Any help? How can I know the total number of pixels from the end of the action bar to where I can't draw anymore.?
In my phone the navigation bar doesn't rotate so it stays where the bottom would be in portrait mode. So the bottom is the end of the screen. However in my tablet the navigation bar rotates and is put at the bottom of the screen.
I found a way to bypass the problem. However it is not neat. I'm posting it here in case anyone else needs it. It is possible to get the width and height by getting those variables form a layout or view whose LayoutParams were set both to MATCHPARENT. Of course this view needs to be that is set to setContentsView, so it should be like the master layout. However this will thrown non-zero values only when the view is already drawn, which means that you can't get its values in methods like on onCreate or onResume.
On the plus side, it should allways work and it is very simple.
I have a progress bar in my action bar, which is set to indeterminate. As you can see in my screenshot my progress bar's width is not 100% of the screen width. it has a margin on both sides. I am using the support libraries and actionbar activity. Is i possible to make it's width to 100% screen width? If yes, how to do that?
what are you looking for is an image of progress bar with different padding ...
see this ... select an progress bar icon from clipart a set different padding for image ... you can see what is padding doing with an image on example at the botton of the page :) this will 100% help you ...
Is it possible to set the width of a single action item in the native action bar?
I have an image that is 100 pixels wide, buts the action bar shrinks it when I add it. I want it to be at full width, kind of like a wrap_content