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
  • 51,611
  • 92
  • 240
  • 358

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
  • 73,337
  • 23
  • 185
  • 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
  • 30,789
  • 13
  • 79
  • 203
sparkymat
  • 9,674
  • 3
  • 29
  • 48
2

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

SBoss
  • 8,495
  • 6
  • 27
  • 44