Getting Screen properties in an Android application

June 28, 2012
By

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="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    >

    <TextView
        android:id="@+id/tv_metrics"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
    />

    </LinearLayout>

5. src/in/wptrafficanalyzer/screendemo/MainActivity.java


package in.wptrafficanalyzer.screendemo;

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

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

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

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

        /** Getting the screen display metrics */
        getWindowManager().getDefaultDisplay().getMetrics(metrics);

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

        /** 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

http://developer.android.com/guide/index.html

How to hire me?

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

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

For hiring me, please mail your requirements to info@wptrafficanalyzer.in.

My other blogs
store4js.blogspot.com


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;

    http://developer.android.com/guide/practices/screens_support.html#dips-pels

    I’m curious to confirm the proper way.

    Thanks.

    • 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.

      Thanks.

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