Can I find out width of screen programmatically in android app? [duplicate]
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开发者_JS百科 part program.
You can get default Display
instance and then read width/height from it:
int width = getWindowManager().getDefaultDisplay().getWidth();
int height = getWindowManager().getDefaultDisplay().getHeight();
You can do this:
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
dm.widthPixels
contains the width and dm.heightPixels
contains the height.
You could also overwrite onSizeChanged(int,int,int,int) in any view and set it in there.
精彩评论