4

Can I find out the width of screen programmatically in android app? I need to paint canvas but its width should be almost like screen and I could not set match_parent in java part program.

Bhavik Kamdar
  • 301
  • 1
  • 7
  • 15
Damir
  • 54,277
  • 94
  • 246
  • 365

3 Answers3

10

You can get default Display instance and then read width/height from it:

int width = getWindowManager().getDefaultDisplay().getWidth();
int height = getWindowManager().getDefaultDisplay().getHeight();
inazaruk
  • 74,247
  • 24
  • 188
  • 156
6

You can do this:

DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);

dm.widthPixels contains the width and dm.heightPixels contains the height.

Nate
  • 31,017
  • 13
  • 83
  • 207
sparkymat
  • 9,938
  • 3
  • 30
  • 48
2

You could also overwrite onSizeChanged(int,int,int,int) in any view and set it in there.

SBoss
  • 8,845
  • 7
  • 28
  • 44