Google+

Monday, September 10, 2012

How to find Screen Size and Density of Device in Android programatically

Hi add this below code in on Create method and check your device or emulator screen configuration.




if ((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_LARGE) {
Toast.makeText(Util.mContext, "Large Screen", Toast.LENGTH_SHORT).show();
Util.v("Screen Size : Large ");

} else if ((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_XLARGE) {
Toast.makeText(Util.mContext, "X Large Screen", Toast.LENGTH_SHORT).show();
Util.v("Screen Size : X Large ");

} else if ((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_NORMAL) {
Toast.makeText(Util.mContext, "Normal Screen", Toast.LENGTH_SHORT).show();
Util.v("Screen Size : Normal ");

}



//Determine density
   DisplayMetrics metrics = new DisplayMetrics();
       getWindowManager().getDefaultDisplay().getMetrics(metrics);
       int density = metrics.densityDpi;

       if (density==DisplayMetrics.DENSITY_HIGH) {
           Toast.makeText(this, "DENSITY_HIGH... Density is " + String.valueOf(density),  Toast.LENGTH_LONG).show();
           Util.v("DENSITY_HIGH... Density is " + String.valueOf(density));
       }
       else if (density==DisplayMetrics.DENSITY_MEDIUM) {
           Toast.makeText(this, "DENSITY_MEDIUM... Density is " + String.valueOf(density),  Toast.LENGTH_LONG).show();
           Util.v("DENSITY_MEDIUM... Density is " + String.valueOf(density));
       }
       else if (density==DisplayMetrics.DENSITY_LOW) {
           Toast.makeText(this, "DENSITY_LOW... Density is " + String.valueOf(density),  Toast.LENGTH_LONG).show();
           Util.v("DENSITY_LOW... Density is " + String.valueOf(density));
       }
       else {
           Toast.makeText(this, "Density is neither HIGH, MEDIUM OR LOW.  Density is " + String.valueOf(density),  Toast.LENGTH_LONG).show();
           Util.v("Density is neither HIGH, MEDIUM OR LOW... Density is " + String.valueOf(density));
       }

1 comment:

Unknown said...

This is really an informative article, and this information you provided about finding screen size and density of device in Android programatically could be pretty helpful to people.

Smartphones

Google+