문제

I'm trying to get the device's screen resolution and print to screen using the below code, however it's throwing a runtime error when trying to execute it. I think this is happening because the "getHeight" and "getWidth" functions are now "deprecated", but not sure how else to get the devices exact screen resolution

        Display display = ((WindowManager)getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();


    TextView w = (TextView) findViewById(R.id.tWidth);
    TextView h = (TextView) findViewById(R.id.tHeight);
    w.setText(Integer.toString(display.getWidth()));
    h.setText(Integer.toString(display.getHeight()));

Thought this would be a relatively simple task, but seems they've changed it in Android Studio. Any suggestions about how to get the above code working, or a different way of getting the screen resolution?

Edit: Updated Title

도움이 되었습니까?

해결책

Use getSize() , you will get in pixels

   Display display =    getWindowManager().getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    int width = size.x;
    int height = size.y;
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top