Getting Screen properties in an Android application

June 28, 2012

In this article, we will create an Android application which will show the screen’s display properties like screen width, screen height and density.

1. Create an Android project namely “ScreenDemo”

New Android Project

Figure 1 : New Android Project

2. Select Android build target

Select Android Build Target

Figure 2: Select Android Build Target

3. Enter application details

Application Details

Figure 3: Application Details

4. res/layout/main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=""



5. src/in/wptrafficanalyzer/screendemo/

package in.wptrafficanalyzer.screendemo;

import android.os.Bundle;
import android.util.DisplayMetrics;
import android.widget.TextView;

public class MainActivity extends Activity {
    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState) {


        /** An object to get Screen display metrics */
        DisplayMetrics metrics = new DisplayMetrics();

        /** Getting the screen display metrics */

        /** Getting reference to TextView object of the main.xml file */
        TextView tvMetrics = (TextView) findViewById(;

        /** Getting the screen density in dpi */
        String density = Integer.toString(metrics.densityDpi);

        /** Getting the screen width and height in pixels */
        String resolution = Integer.toString(metrics.widthPixels) + "x" + Integer.toString(metrics.heightPixels);

        /** Converting Screen resolution in pixels into dp */
        float dp_w = ( metrics.widthPixels * 160 ) / metrics.xdpi;

        /** Converting Screen resolution in pixels into dp */
        float dp_h = ( metrics.heightPixels * 160 ) / metrics.ydpi;

        /** Getting the screen width and height in dp */
        String resolution_dp = Float.toString((int)dp_w) + "x" + Float.toString((int)dp_h);

        /** Setting the density, width and height of the screen to the TextView object */
        tvMetrics.setText("Density : "+density + "\n" + "Resolution in pixels : " + resolution  + "\nResolution in dp : " + resolution_dp );


6. Application in execution

Application in Execution

Figure 4: Application in Execution

7. Download

8. Reference

How to hire me?

I am George Mathew, working as software architect and Android app developer at

You can hire me on hourly basis or on project basis for Android applications development.

For hiring me, please mail your requirements to

My other blogs

Android Knowledge Quiz

Ready to test your knowledge in Android? Take this quiz :

Tags: ,

2 Responses to Getting Screen properties in an Android application

  1. LR on August 11, 2012 at 5:20 am

    I feel like this isn’t the correct way to convert pixels to dp’s. Someone explain to me the logic if I’m wrong. For one, why multiply by 160 and not metrics.density?

    Shouldn’t it be:
    float dp_w = metrics.widthPixels / metrics.density;

    I’m curious to confirm the proper way.


    • LR on August 15, 2012 at 7:33 am

      After looking at the screenshot of the device, the printed values look correct. The calculation must be accurate, then, but I think it’s not the cleanest approach.


Leave a Reply

Your email address will not be published. Required fields are marked *

Be friend at g+

Subscribe for Lastest Updates

FBFPowered by ®Google Feedburner