标签云

微信群

扫码加入我们

WeChat QR Code

I created some custom elements, and I want to programmatically place them to the upper right corner (n pixels from the top edge and m pixels from the right edge). Therefore I need to get the screen width and screen height and then set position:int px = screenWidth - m;int py = screenHeight - n;How do I get screenWidth and screenHeight in the main Activity?


Use dp instead of px. because it will distort your layout with other devices..

2019年05月23日54分50秒

Don't forget to multiply by getResources().getDisplayMetrics().density to account for display scaling

2019年05月23日54分50秒

This proves the most up-voted answer is not always the best (& lots of people repeat answers for rep). Instead of getSize and deprecated getWidth/getHeight combo (ignoring errors), try Balaji.K's getMetrics. Nik's comment in his answer even explains getDisplayMetrics to consider the system/status bar size. Also you may use density as jmaculate and LoungeKatt explained to have the EXACT value:DisplayMetrics dm = getResources().getDisplayMetrics(); float fwidth = dm.density * dm.widthPixels; Tested in Android v2.2 (API 8) and v4.0 with good results and no errors/warnings.

2019年05月24日54分50秒

DisplayMetrics displaymetrics = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(displaymetrics); int height = displaymetrics.heightPixels; int width = displaymetrics.widthPixels;

2019年05月24日54分50秒

another way to get the DisplayMetrics : Resources.getSystem().getDisplayMetrics(). You won't need a Context to get them.

2019年05月24日54分50秒

developer.android.com/reference/android/view/Display.html

2019年05月24日54分50秒

getWidth() or getSize()?What would I use if I need my app to run on API <13 as well as API >13?

2019年05月24日54分50秒

try { display.getSize(size); width = size.x; height = size.y; } catch (NoSuchMethodError e) { width = display.getWidth(); height = display.getHeight(); }

2019年05月23日54分50秒

I don't see why you want to use try/catch for such a check? Why not simply use if (android.os.Build.VERSION.SDK_INT >= 13) without any thrown exception at all? I think of try/catch in a normal app-flow as bad practice.

2019年05月24日54分50秒

what if you want to get the screen size excluding the navigation bar and/or notification bar

2019年05月24日54分50秒

Note that the metrics (width, height) change depending on the rotation of the device.

2019年05月24日54分50秒

Metrics will return the size of the display, but HoneyComb and up, your activity will have less space than what's returned due to the system bar.

2019年05月24日54分50秒

Guy it depends on your implementation. You can hide the status bar: requestWindowFeature(Window.FEATURE_NO_TITLE); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);

2019年05月23日54分50秒

Also exists this way: getContext().getResources().getDisplayMetrics().widthPixels

2019年05月24日54分50秒

final float scale = getResources().getDisplayMetrics().density; int width = (int) (metrics.widthPixels * scale + 0.5f); - You have to account for device density when doing it that way.

2019年05月24日54分50秒

or just write view.post

2019年05月23日54分50秒

a gist for quick implementation :) gist.github.com/doridori/9208247

2019年05月23日54分50秒

view.post is not guaranteed to work though. It is just a workaround.

2019年05月24日54分50秒

marsbear where does it say View.post() isn't guaranteed to work? I'm curious because I also rely on this method for getting a View's size after layout and was unaware it unreliable.

2019年05月24日54分50秒

Turbo I say that :p We used that workaround before and it turned out to be unreliable. That workaround is based on the assumption, that theinitially layouting is done within the same UIThread turn as the initialization. Hence you could schedule your code to run before the next UI turn via post() and be fine. It turned out that the layouting might take even longer under certain circumstances and the view was still not layouted when the posted code ran. What I do now is add the ViewTreeObserver in onCreate and remove it after the first onLayout. Init your stuff during that first onLayout.

2019年05月24日54分50秒

Yeah, this was back in 2012.

2019年05月23日54分50秒

sorry, allow me to clarify what I meant.It is highly unlikely that anyone would support API <14 as of 6/22/2015

2019年05月24日54分50秒

This is the only post here that takes into account the window decorations (statusbar/menu bar).worked great for me.

2019年05月24日54分50秒

Awesome, however you should never have expected business logic fire exceptions. Exception firing(even when caught) is horrible for performance. Plus you are doing more work than needed if the SDK_INT is > 13. Instead, you should just add some ifs on the Build.VERSION.SDK_INT.

2019年05月24日54分50秒

Erik B, I agree. I added improved version. Still I left the part "since SDK 1" just in case something goes wrong in try/catch (I've seen many bad things in Android especially when you think it's impossible something to go wrong so I like to keep it safe :)). Anyway there shouldn't be too mouch overhead since this calculation should be done only once (e.g. values could be kept in some static attributes).

2019年05月23日54分50秒

Note that this code is licensed under GPL v3 which has implications for using in production apps.

2019年05月24日54分50秒

GPL and depending on reflection to call methods which might not be present in future Android versions. Mhhh

2019年05月23日54分50秒

Just what i needed! getResources().getDisplayMetrics().heightPixels + result gives actual fullscreen height. Dragan Marjanović's answer also works but I prefer this much shorter and simpler solution.

2019年05月24日54分50秒

This approach is outdated as of Android Marshmallow. Status bar height can change from device or Android version. Better use OnApplyWindowInsetsListener.

2019年05月24日54分50秒

This is actually how the documentation tells you to do it... but it's kinda pointless if you are NOT using a view. you may be using something else, e.g. mglsurfaceview.

2019年05月24日54分50秒

this is better as the parent view may not be the size of the display. But make sure that this is done at a point where the view you are querying the size of has already been layed out, which will not usually be inside onCreate(). You can use a custom callback from onWindowFocusChanged(boolean hasFocus) which will be called after all the views have been measured and so on, ensuring you will not receive incorrect dimensions for the view you are interested it...

2019年05月24日54分50秒

You need to add SuppressLint("NewApi") just above the function signature in order to be able to compile.

2019年05月23日54分50秒

I think it's better to get the height and width in one go rather than in separate calls to the APIs because otherwise they could be out of sync. This could happen if the screen orientation is changing while your code is running.

2019年05月23日54分50秒

It's not subtracting space for on-screen controls (such as on Tablets or Nexus devices).Also, I get a different value (750 vs 800) on my 10" Tablet, depending on whether the Activity is active when the calculation is done. But for my purposes, this is more than fine. Thank you!

2019年05月23日54分50秒

Cool but the is one scary thing about this method in the sourcecode: this comment

2019年05月23日54分50秒

This crashes on a emulator running Android 4.0 (API 14).

2019年05月24日54分50秒

mContext.getResources().getDisplayMetrics().widthPixels;???

2019年05月24日54分50秒

Obviously, you can also pass back the height in the bundle too.

2019年05月23日54分50秒

nopes...it returns 0.

2019年05月24日54分50秒