Android: check device screen density programatically

11 01 2014

When designing app for multiple screen sizes it is useful to check screen density (ldpi, mdpi, hdpi, xhdpi) and to know exact value in dpi.

Source: Android Developers

Source: Android Developers

This is how to do it programatically:

public void determineScreenDensity() {
	DisplayMetrics metrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(metrics);
    int density = metrics.densityDpi;

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

Did I help you?
I manage this blog and share my knowledge for free, sacrificing my time. If you appreciate it and find this information helpful, please consider making a donation in order to keep this page alive and improve quality

Donate Button with Credit Cards

Thank You!

Advertisements

Actions

Information

Give Your feedback:

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s




%d bloggers like this: