Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

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;
  DisplayMetrics size = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(size );
        int w = size .widthPixels;
        int h = size .heightPixels;
WindowManager window = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics dm = new DisplayMetrics();
window.getDefaultDisplay().getMetrics(dm);          

devicewidth = (dm.widthPixels ) ;

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top