How do I get the ScreenSize programmatically in android
Solution 1:
Copy and paste this code into your Activity
and when it is executed it will Toast
the device's screen size category.
int screenSize = getResources().getConfiguration().screenLayout &
Configuration.SCREENLAYOUT_SIZE_MASK;
String toastMsg;
switch(screenSize) {
case Configuration.SCREENLAYOUT_SIZE_LARGE:
toastMsg = "Large screen";
break;
case Configuration.SCREENLAYOUT_SIZE_NORMAL:
toastMsg = "Normal screen";
break;
case Configuration.SCREENLAYOUT_SIZE_SMALL:
toastMsg = "Small screen";
break;
default:
toastMsg = "Screen size is neither large, normal or small";
}
Toast.makeText(this, toastMsg, Toast.LENGTH_LONG).show();
Solution 2:
private static String getScreenResolution(Context context)
{
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
DisplayMetrics metrics = new DisplayMetrics();
display.getMetrics(metrics);
int width = metrics.widthPixels;
int height = metrics.heightPixels;
return "{" + width + "," + height + "}";
}
Solution 3:
Determine Screen Size :
int screenSize = getResources().getConfiguration().screenLayout &Configuration.SCREENLAYOUT_SIZE_MASK;
switch(screenSize) {
case Configuration.SCREENLAYOUT_SIZE_LARGE:
Toast.makeText(this, "Large screen",Toast.LENGTH_LONG).show();
break;
case Configuration.SCREENLAYOUT_SIZE_NORMAL:
Toast.makeText(this, "Normal screen",Toast.LENGTH_LONG).show();
break;
case Configuration.SCREENLAYOUT_SIZE_SMALL:
Toast.makeText(this, "Small screen",Toast.LENGTH_LONG).show();
break;
default:
Toast.makeText(this, "Screen size is neither large, normal or small" , Toast.LENGTH_LONG).show();
}
Determine density:
int density= getResources().getDisplayMetrics().densityDpi;
switch(density)
{
case DisplayMetrics.DENSITY_LOW:
Toast.makeText(context, "LDPI", Toast.LENGTH_SHORT).show();
break;
case DisplayMetrics.DENSITY_MEDIUM:
Toast.makeText(context, "MDPI", Toast.LENGTH_SHORT).show();
break;
case DisplayMetrics.DENSITY_HIGH:
Toast.makeText(context, "HDPI", Toast.LENGTH_SHORT).show();
break;
case DisplayMetrics.DENSITY_XHIGH:
Toast.makeText(context, "XHDPI", Toast.LENGTH_SHORT).show();
break;
}
for Ref: http://devl-android.blogspot.in/2013/10/wifi-connectivity-and-hotspot-in-android.html
Solution 4:
I think it is a pretty straight forward simple piece of code!
public Map<String, Integer> deriveMetrics(Activity activity) {
try {
DisplayMetrics metrics = new DisplayMetrics();
if (activity != null) {
activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
}
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("screenWidth", Integer.valueOf(metrics.widthPixels));
map.put("screenHeight", Integer.valueOf(metrics.heightPixels));
map.put("screenDensity", Integer.valueOf(metrics.densityDpi));
return map;
} catch (Exception err) {
; // just use zero values
return null;
}
}
This method now can be used anywhere independently. Wherever you want to get information about device screen do it as follows:
Map<String, Integer> map = deriveMetrics2(this);
map.get("screenWidth");
map.get("screenHeight");
map.get("screenDensity");
Hope this might be helpful to someone out there and may find it easier to use. If I need to re-correct or improve please don't hesitate to let me know! :-)
Cheers!!!
Solution 5:
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int width = displayMetrics.widthPixels;
int height = displayMetrics.heightPixels;