Home > Tricks & Tips > Retrieve monitor screen resolution

Retrieve monitor screen resolution

After a while learning from Internet, I’ve found the other two methods to retrieve monitor screen resolution, comparing to my previous post on retrieving monitor resolution.

Here the solution:

package pete.android.study;

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

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

        TextView tvResult = (TextView)findViewById(R.id.tvResult);

        // get display metrics from resources
        DisplayMetrics metricsA = getResources().getDisplayMetrics();

        // get display metrics from display
        Display display = getWindowManager().getDefaultDisplay();
        DisplayMetrics metricsB = new DisplayMetrics();

        		"[ Using Resources ] \n" +
            	"Width: " + metricsA.widthPixels + "\n" +
            	"Height: " + metricsA.heightPixels + "\n"
            	+ "\n" +
            	"[ Using Display ] \n" +
            	"Width: " + metricsB.widthPixels + "\n" +
            	"Height: " + metricsB.heightPixels + "\n"

Both gives the same results.

Monitor Resolution

Monitor Resolution

Hope you enjoy it!


Pete Houston

  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

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 )

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s

%d bloggers like this: