7

Hi I have a problem creating a custom view for an Android application. My custom view wants to use if permitted the maximum screen width. I couldn't find any way to retrieve this value.

Can anybody point me to the right method?

sulf
  • 225
  • 1
  • 3
  • 9

4 Answers4

15

Try

mWinMgr = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
int displayWidth = mWinMgr.getDefaultDisplay().getWidth();

where context is Context instance.

By default, the FrameLayout in which your layout is kept, fills the whole display horizontally (vertically you can have status bar). So you can set the maximum possible width by using android:layout_width="fill_parent" correctly.

Dimitar Dimitrov
  • 16,032
  • 5
  • 53
  • 55
2

Here is a good answer:

mWinMgr.getDefaultDisplay().getWidth();// getWidth() and getHeight() methods are deprecated now

You can get Width and height as follows: original

Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
Community
  • 1
  • 1
Muhammad Nabeel Arif
  • 19,140
  • 8
  • 51
  • 70
1
  DisplayMetrics size = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(size );
        int w = size .widthPixels;
        int h = size .heightPixels;
1
WindowManager window = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics dm = new DisplayMetrics();
window.getDefaultDisplay().getMetrics(dm);          

devicewidth = (dm.widthPixels ) ;