I have used the followng ways to get screen width and height. When I run my application in motorola I got width= 320pixel and height = 533 pixel. But size of motorla milestone is 480 x 854 pixels. H开发者_如何学编程ow to get height as 854 and width as 480 see http://www.gsmarena.com/motorola_milestone-3001.php
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int heightOfScreen = metrics.heightPixels;
int widthOfScreen = metrics.widthPixels;
System.out.println("...w1..."+width+"...h1..."+height+"....w2...."+widthOfScreen+"...h2..."+heightOfScreen);
Output w1=320...h1=533...w2=320...h2=533
I have not tried this, but from what you have tried so far I'm willing to bet this will work:
DisplayMetrics metrics = getResources().getDisplayMetrics();
float width = metrics.widthPixels * metrics.xdpi;
float height = metrics.heightPixels * metrics.ydpi;
Edit: you can also try Display.getSize
.
Second edit: scratch that. My first solution is incorrect and my second solution is API Level 13+ only. However, I have tried both your methods and they worked correctly on a Huawei Ideos X5 (Android 2.2) and a Nexus S (Android 2.3.4). It might just be Motorola's fault here. Are there any official updates available for your phone's OS?
I have tried with the following approach. It worked for me.
WindowManager windowManager = (WindowManager) activity.getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
int height = windowManager.getDefaultDisplay().getHeight();
int width = windowManager.getDefaultDisplay().getWidth();
Try following
Display display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
Log.i(TAG, "Width " + display.getWidth() + "\t\t Height :" + display.getHeight());
精彩评论