Getting the actual screen height (android) - java

I want to get the actual screen height of the device that runs my app. To achieve this i try the following:
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int Height = metrics.heightPixels;
TextView HeightView = (TextView) findViewById(R.id.screenHeight);
HeightView.setText("Screen Height: " + Height);
int Width = metrics.widthPixels;
TextView WidthView = (TextView) findViewById(R.id.screenWidth);
WidthView.setText("Screen Width: " + Width);
The device that I run using the emulator has a screen width of 1080 pixels and a screen height of 1920 pixels. The width is displayed correctly (1080 pixels) but the height is according to the app only 1776 pixels. What do I need to make my app display the correct screen height? Is metrics.heightPixels a bad way of getting the screen height?

see this answer
if your API level > 13 try this if you are in activity
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
If you're not in an Activity you can get the default Display via WINDOW_SERVICE:
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
getWidth and getHeight is for API level 13 or less
UPDATE
For API 17 and higher method Display.getRealSize() returns full size of screen, as mentioned in documentation:
Gets the real size of the display
without subtracting any window decor or applying any compatibility
scale factors.
The size is adjusted based on the current rotation of the display.
The real size may be smaller than the physical size of the screen when
the window manager is emulating a smaller display (using adb shell am
display-size).

int width=Resources.getSystem().getDisplayMetrics().widthPixels;
int height=Resources.getSystem().getDisplayMetrics().heightPixels;

I needed to get the actual available height, and out of all the options this is the only thing that worked for me.
Create a rectangle, and get its dimensions:
Rect r = new Rect();
activityRootView.getWindowVisibleDisplayFrame(r);
int screenHeight = r.bottom - r.top;
where activityRootView is the root view of your layout.
If you need to extract the toolbar height:
int availableScreenHeight = screenHeight - toolbar.getHeight();
where toolbar is your toolbar view.
availableScreenHeight will now be without the statusbar, without the toolbar, and without the navigation bar (if the device is using it).

you can use this code:
val display = windowManager.defaultDisplay
val size = Point()
display.getSize(size)
val width: Int = size.x
val height: Int = size.y

You can try this:
WindowManager wm = getWindowManager();
Display d = wm.getDefaultDisplay();
Point size = new Point();
d.getSize(size);
Log.i("LOG", "W=" + size.x + " , H=" + size.y);

Related

Android layout for specific device not show up

I prepared for my two devices two different layouts. First I checked the width and height with this code:
Display display = getWindowManager().getDefaultDisplay();
DisplayMetrics outMetrics = new DisplayMetrics ();
display.getMetrics(outMetrics);
float density = getResources().getDisplayMetrics().density;
float dpHeight = outMetrics.heightPixels / density;
float dpWidth = outMetrics.widthPixels / density;
It showed me that, first device has 360dp width and 592dp height and the second 360dp width and 692dp height. So I prepared two layouts in such folders layout-w360dp-h592dp and layout-w360dp-h692dp.
And it turns out the devices don't use the layout that they are dedicated to. 360dp width and 592dp height device uses default layout and 360dp width and 692dp height device use layout for 360dp width and 592dp height.
Where is the problem? Thank you in advance!

How to get the middle of the screen for API>=19

The button should move to the middle of the screen when clicked. But every time I get different position of button in different devices. And I don't know how can I fix it.
This code what I use:
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getRealMetrics(metrics);
float middleScreen = metrics.xdpi;
final TranslateAnimation animation = new TranslateAnimation(0, -middleScreen, 0, 0);
animation.setDuration(3000);
animation.setFillAfter(true);
buttonToNextView.setAnimation(animation);
First of all, what DisplayMetrics.xdpi gave us is the exact physical pixels per inch of the screen in the X dimension which is not the number of pixels on the x-axis.
So, we should use half of widthPixels and heightPixels (based on screen orientation) to achieve the middle of the screen on the x-axis.
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getRealMetrics(metrics);
boolean isDisplayPortrait = getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT;
float middleScreen = isDisplayPortrait ? metrics.widthPixels / 2 : metrics.heightPixels / 2;
final TranslateAnimation animation = new TranslateAnimation(0, -middleScreen, 0, 0);
animation.setDuration(3000);
animation.setFillAfter(true);
buttonToNextView.setAnimation(animation);
Notice that to place the button exactly at the middle of the screen, you need to subtract half of its width from the middleScreen value.

Android getDisplayMetrics() not returning accurate display size

I'm trying to get the pixel width/height of the device for placing elements and mapping touch events on the screen but not getting the desired effect (notice lower right square should be completely flush with the bottom right):
// screen size
DisplayMetrics metrics = Resources.getSystem().getDisplayMetrics();
float ScrDensity = metrics.density;
int metricHeight = metrics.heightPixels;//
int metricWidth = metrics.widthPixels;//
Log.i("AllMetrics", metrics.toString());
// temp
int rightButton_diameter = 100;
int rightButton_xpos = metricWidth - rightButton_diameter;
int rightButton_ypos = metricHeight - rightButton_diameter;
Where AllMetrics returns:
DisplayMetrics{density=2.0, width=720, height=1184, scaledDensity=2.0, xdpi=320.0, ydpi=320.0}
I notice that the returned height is also different from the 720x1280 listed in the Virtual Device Manager (API 19, Android 4.4) and the x/y positions of the touch events seems accurate to the drawn elements (ie the clickable area of the button is the same as where it is being drawn)
Is this a configuration error or am I using the incorrect method of fetching screen position?
Check this code it works for me
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels;
int height = dm.heightPixels;
Outside of activity you can try this
((Activity) getContext()).getWindowManager().getDefaultDisplay().getMetrics(dm)
To access the DisplayMetrics members, initialize an object like this:
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);

Android – Am I Doing this Right?

I am building an android app that requires a layout to be added programmatically. This is the line that seems to be giving me a hard time:
RelativeLayout.LayoutParams rightScreenParams = new RelativeLayout.LayoutParams(
(int) (screenWidth * 0.25), (int) (screenHeight * 0.94));
The variables screenWidth and screenHeight are the width and height of the screen in pixels. The layout is supposed to be 25% of the width of the parent layout, and 94% of the height. When I add the parameters to the layout and then add the layout, it takes up the entire screen instead of the dimensions I mentioned.
So am I using this constructor correctly? Any help is appreciated. Thank you for your time.
Edit:
I get screenWidth and screenHeight in onCreate():
DisplayMetrics displayMetrics = new DisplayMetrics();
WindowManager wm = (WindowManager) getApplicationContext()
.getSystemService(Context.WINDOW_SERVICE);
wm.getDefaultDisplay().getMetrics(displayMetrics);
screenWidth = displayMetrics.widthPixels;
screenHeight = displayMetrics.heightPixels
The variable screenHeight is returning as 1184 even though the actual height in pixels is 1280. The variable screenWidth seems to be fine. So how can I get an actual screen height in pixels?
RelativeLayout.LayoutParams rightScreenParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.Wrap_Parent,samehere);
View.setLayoutParams(rightScreenParams);//your view
//after you add it to the layout then you call
rightScreenParams.width = (int) (screenWidth * 0.25);
rightScreenParams.height = (int) (screenHeight * 0.94);
its the same thing but it forces android.

android get available height and width for portrait and landscape

so I have written the following method in my activity:
private void setDisplayMetrics(){
DisplayMetrics metrics = this.getResources().getDisplayMetrics();
int dh = metrics.heightPixels;
int dw = metrics.widthPixels;
if(dw < dh){
deviceWidth = dw;
deviceHeight = dh;
}else{
deviceWidth = dh;
deviceHeight = dw;
}
System.err.println("--------------> dh : "+deviceHeight+" | dw "+deviceWidth);
}
And it works great, in the sense that it gets me the total width and height of the screen with great accuracy and reliability (which is what I have asked it to do).
Here is the problem. On older android devices the screen dimensions are the same as the dimensions the application can take up, and the script above helps me to set the size of elements in the app. BUT with android ICS I have this graphic button bar on the bottom of the screen, and it messes up my whole strategy.
What I would really like is the ability to get the available app dimensions for the portrait view as well as the landscape view at the same time in one method. And have these dimensions be accurate regardless of the presence of the bar pictured above.
Does anyone know how to achieve this?
Rect rectgle= new Rect();
Window window= getWindow();
window.getDecorView().getWindowVisibleDisplayFrame(rectgle);
int StatusBarHeight= rectgle.top;
int contentViewTop=
window.findViewById(Window.ID_ANDROID_CONTENT).getTop();
int TitleBarHeight= contentViewTop - StatusBarHeight;
Log.i("*** Jorgesys :: ", "StatusBar Height= " + StatusBarHeight + " , TitleBar Height = " + TitleBarHeight);
From what I've read so far you can't get this height directly. The approach via getWindowVisibleDisplayFrame() does not seem to work.
The only promising solution I have found so far is using a custom layout to measure the screen size as explained in
http://evgeni-shafran.blogspot.de/2011/01/android-screen-size-problem.html.
I am convinced that this will work but to me it seems to be more trouble than it is worth.

Categories