Search This Blog

Showing posts with label DisplayMetrics. Show all posts
Showing posts with label DisplayMetrics. Show all posts

Monday, January 23, 2012

Get an Display Resolution of the Screen

android.util.DisplayMetrics is a structure describing general information about a display, such as its size, density, and font scaling.

To use android.util.DisplayMetrics to get resolution:

Create a dummy Android Application, add a TextView in main.xml to display the result.

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

Modify the .java file to read DisplayMetrics

 public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.main);
     
            DisplayMetrics dm = new DisplayMetrics();
            getWindowManager().getDefaultDisplay().getMetrics(dm);
            String str_ScreenSize = "The Android Screen is: "
                        + dm.widthPixels
                        + " x "
                        + dm.heightPixels;

            TextView mScreenSize = (TextView) findViewById(R.id.strScreenSize);
            mScreenSize.setText(str_ScreenSize);
     }