5

I'm making game for Android and use SurfaceView.

I want to know SmartPhone Screen Info(Height, Width, ect..)

I uesd this code, but.. It is not correct value of display Width and Height,

It's always printed "0" by Red Color

What I miss?

public class MainActivity extends Activity {
private int width;
private int height;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

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

    setContentView(new GameView(this));
}

public class Painter extends Thread
public void run(){
    Canvas canvas = null;
    long start, end;
    int sleep;
     while(true) {
         if (runnable) {
             start = System.currentTimeMillis();

             try {
                 canvas = holder.lockCanvas(null);
                 paint.setColor(Color.RED);
                 paint.setTextSize(200);

                 synchronized (holder) {
                     canvas.drawText(Integer.toString(main.getHeight()), 600, 800, paint);
                     canvas.drawText(Integer.toString(main.getWidth()), 300, 400, paint);
                 }
             } catch (Exception e) {
                 e.printStackTrace();
             } finally {
                 holder.unlockCanvasAndPost(canvas);
             }

             end = System.currentTimeMillis();

             sleep = 1000 / FPS - (int) (end - start);
             try {
                 Thread.sleep(sleep > 0 ? sleep : 0);
             } // Try
             catch (InterruptedException e) {
                 e.printStackTrace();
             }
         }
     }
KIM
  • 233
  • 1
  • 3
  • 10

4 Answers4

4

You can get it using this Code:

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

Works on all API versions, and therefore requires no API checking. If you are in a view, you might need to get the context:

((Activity) getContext()).getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);‌​
Irina Avram
  • 1,492
  • 2
  • 20
  • 35
1

You can get Display height and width using following code.

 Display display = getWindowManager().getDefaultDisplay();
 Point size = new Point();
 display.getSize(size);
 int width = size.x;
 int height = size.y;
Chirag Savsani
  • 6,020
  • 4
  • 38
  • 74
1
 /*
        * A structure describing general information about a display, such as its size, density, and font scaling.
        * */
        DisplayMetrics metrics = getResources().getDisplayMetrics();

       int DeviceTotalWidth = metrics.widthPixels;
       int DeviceTotalHeight = metrics.heightPixels;

How to Get Device Height and Width at Runtime?

Community
  • 1
  • 1
IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198
  • 3
    Instead of linking to a question that is about exactly the same, vote to close it as duplicate – Tim Dec 16 '15 at 08:43
0
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);
Saurabh sharma
  • 369
  • 2
  • 13