user2819789
user2819789

Reputation: 11

Determining Screen Density

Brand new to stackoverflow, haven't figured out how to ask a question within a thread.

This is for thread: How to find the device as LDPI MDPI HDPI or XHDPI

The code used is:

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

My phone (LG Optimus L5) reports MDPI (160) even though tech specs state 144dpi, which is fine since it works perfectly with the code. However, my Google Nexus 7 reports 213dpi which doesn't match a constant and I can't find a reference to borders of densities.

A work around (I believe, new to Android) would be to put a hidden constant in the layouts in the density layout folders and check the constant in code to see which density constant is being used.

The question is: Can I determine this value within code without the constant in the layouts with devices reporting values not matching the constants?

Thanks

Upvotes: 1

Views: 1111

Answers (2)

tilpner
tilpner

Reputation: 4351

I also own a Google Nexus 7 and I know it has tvdpi density.

Upvotes: 0

njzk2
njzk2

Reputation: 39386

A report of 213 is DENSITY_TV.

See http://developer.android.com/reference/android/util/DisplayMetrics.html#DENSITY_TV

Upvotes: 1

Related Questions