How to get unused window height in android - android

I want to get the total unused window height, which my application can not consume from total device height, like- status bar or any other decorated views.

First get the total height of the screen using display metrics. Then find the height of status bar and action bar (if you are using) and subtract these from screen height.

Related

When in multi-window mode, Window height does not include status bar height

My activity draws under the status bar, which is translucent.
I am getting the window height using:
WindowManager manager = activity.getWindowManager();
DisplayMetrics metrics = new DisplayMetrics();
manager.getDefaultDisplay().getMetrics(metrics);
int height = metrics.heightPixels;
This height is correct, in that includes the content height and the status bar height, since I’m drawing there and my window has the correct flags.
When in multi-window mode, in Nougat, if my app is the top one, it is still drawing under the status bar, but height will not include that value.
How do I get the real window height in multi-window mode without getting dirty?
Note
I don’t want the screen height, as returned e.g. by Display.getRealSize(). I want the Window height. But if my window draws under the status bar, the value must include the status bar height, as it does when multi-window is off.
More info
Actually, after going multi-window, the height value will never have the status bar included, even if you exit multi-window after. For instance, on a Nexus5X:
// Start the app in normal mode
height = 1794; // Everything except nav bar. OK!
// Go in multi-window mode.
// Go out of multi-window mode.
height = 1730; // Status bar height is not there anymore. :-(
Seems like a dirty dirty bug. In that case I wonder if anyone knows more about it (devices, API levels, bug report).
There is an AOSP Issue (219133) about problems with the Display class when using multi-window mode in the Android 7.0 preview.
The development team states they fixed the error and the patch "will be available in future releases". So far (version 7.0 and 7.1), the buggy behavior persists.

Default size of action item in actionbar in percent

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.

Effectively measure the available screen

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.

Progress Bar in Action Bar: Make width fill screenwidth

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 ...

Android: Split ActionBar real parent height

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.

Categories

Resources