简体   繁体   中英

How do I get a device's maximal width and height in android

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?

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.

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 ) ;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM