ItGo.me Focus on IT Recommend

Home > android - Get screen dimensions in pixels

android - Get screen dimensions in pixels

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?

Recommend:Android Get Screen dimensions from Service

wManager().getDefaultDisplay(); But the same thing is not working from a service (for obvious reasons, i know!) but i desperately need to get the display size from a service. Can some1 please explain me any other way for getting the Screen

android layout screen pixel dimensions
|
  this question
edited Jul 3 '16 at 10:43 Ironman 5,845 4 10 39 asked Jun 19 '09 at 8:25 Niko Gamulin 28.5k 81 186 251      Use dp instead of px. because it will distort your layout with other devices.. –  Nepster Apr 24 '14 at 6:13      Don't forget to multiply by getResources().getDisplayMetrics().density to account for display scaling –  jmaculate Jun 16 '14 at 12:53      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. –  Armfoot Sep 2 '14 at 10:32      DisplayMetrics displaymetrics = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(displaymet‌​rics); int height = displaymetrics.heightPixels; int width = displaymetrics.widthPixels; –  Azahar May 1 '15 at 5:30

 | 

31 Answers
31

prev 1 2

This function returns the approximate screen size in inches.

public double getScreenSize()
{
        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        int width=dm.widthPixels;
        int height=dm.heightPixels;
        int dens=dm.densityDpi;
        double wi=(double)width/(double)dens;
        double hi=(double)height/(double)dens;
        double x = Math.pow(wi,2);
        double y = Math.pow(hi,2);
        double screenInches = Math.sqrt(x+y);
        return screenInches;
}

|
  this answer
answered Jan 5 '16 at 5:31 Abhishek 626 5 12

 |  prev 1 2

protected by Community♦ Aug 10 '11 at 22:14

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged android layout screen pixel dimensions or ask your own question.

Recommend:Get screen Dimensions in Android

size for all API levels Kindly help me. answer 1 try with this dude int density; private DisplayMetrics metrics; private int widthPixels; private float scaleFactor; private float widthDp;private DisplayMetrics metrics;

oriUlr:http://stackoverflow.com/questions/1016896/get-screen-dimensions-in-pixels?page=2&tab=votes

------splitte line----------------------------