How can I get the available height of the screen in Android? I need to the height minus the status bar / menu bar or any other decorations that might be on screen and I need it
Working solution:
Create two dummy views :
<View
android:id="@+id/top"
android:layout_height="0dp"
android:layout_width="match_parent"
android:layout_alignParentTop="true" />
<View
android:id="@+id/bottom"
android:layout_height="0dp"
android:layout_width="match_parent"
android:layout_alignParentBottom="true" />
Now, you should call getLocationOnScreen(int [])
on these two views to receive their locations. This function returns an array of 2 integers. The first being x and the second y. We require only y. Also note that correct values are returned only AFTER all the views are created and the onCreate
method is exited.
I tested this code on a regular fragment called from a viewpager.
The code was in the onCreateView
method of my fragment. I still had to use a postDelayed
to get the correct values.
final View top = (View) view.findViewById(R.id.top);
final View bottom = (View) view.findViewById(R.id.bottom);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
int topLoc[] = new int[2];
top.getLocationOnScreen(topLoc);
int BottomLoc[] = new int[2];
bottom.getLocationOnScreen(BottomLoc);
Log.d(Constants.debug, "topY: "+ topLoc[1]+" BottomY:" + BottomLoc[1]);
}
},4000);
I have a Nexus 4.
In regular mode (status bar and the soft buttons shown) :
07-05 02:28:23.367 565-565/com.torcellite.xx D/xx: topY: 50 BottomY:1182
In full screen mode (no status bar or soft buttons shown) :
07-05 02:29:17.360 707-707/com.torcellite.xx D/xx: topY: 0 BottomY:1280
All you have to do now is subtract.
Get the height of the root layout that is loaded by the activity. In case it does not fill the screen put it inside another layout with a height property of fill_parent.
It may not answer your question, but it could be useful to know (I was looking for it myself when I came to this question) that if you need a View's dimension but your code is being executed when its layout has not been laid out yet (for example in onCreate() ) you can setup a ViewTreeObserver.OnGlobalLayoutListener with View.getViewTreeObserver().addOnGlobalLayoutListener() and put the relevant code that needs the view's dimension there. The listener's callback will be called when the layout will have been laid out.
courtesy-Francesco Feltrinelli
You could try to do this.
For Display
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int height = displaymetrics.heightPixels;
int width = displaymetrics.widthPixels;
For Status Bar
public int getStatusBarHeight() {
int result = 0;
int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
result = getResources().getDimensionPixelSize(resourceId);
}
return result;
}
I haven't tried different API's but I'm using these values to figure out whether or not the onscreen keyboard is up. Maybe you can try this and something comparable for width to get what you need?
Rect r = new Rect();
view.getWindowVisibleDisplayFrame(r);
int
screenHeight = view.getRootView().getHeight(), // height of the entire screen
appHeight = r.bottom - r.top, // height of the entire app
statusBarHeight = r.top, // height of the statusbar
pageHeight = view.getHeight(), // height of the app without title
appTitleHeight = appHeight - pageHeight, // height of the only the app title
topHeight = statusBarHeight + appTitleHeight;
How about setting up a subclassed View as the root of the Activity. Make this View fill the screen (it won't go past the status bar) and override onSizeChanged(). Store those values and write a getter for them.