How to Get Device Height and Width at Runtime?

前端 未结 6 368
执念已碎
执念已碎 2020-12-09 15:06

I am developing an app in which I have to make our app to fit for every device - for both tablet and android mobiles. Now I want to get the device height and width at runtim

6条回答
  •  庸人自扰
    2020-12-09 16:02

    Display mDisplay = activity.getWindowManager().getDefaultDisplay();
    final int width  = mDisplay.getWidth();
    final int height = mDisplay.getHeight();
    

    This way you can get the screen size.

    Since this API is depricated in the new SDK versions you can use this.

    DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
    int width = displayMetrics.widthPixels;
    int height = displayMetrics.heightPixels;
    

提交回复
热议问题