Detect Screen Size

PDF for offline use
Sample Code:
Related Articles:
Related SDKs:

Let us know how you feel about this

Translation Quality


0/250

This recipe will show how to detect the screen size of a device, in density-independent pixels.

Recipe

  1. Create a new Xamarin.Android application named ScreenSize .
  2. Edit Main.axml so that it contains two TextViews:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView
        android:text="Screen Width:"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/screenWidthDp" />
    <TextView
        android:text="Screen Height:"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/screenHeightDp" />
</LinearLayout>
  1. Edit `Activity1.cs`, change the code in `OnCreate` to the following:
protected override void OnCreate(Bundle bundle)
{
    base.OnCreate(bundle);
    SetContentView(Resource.Layout.Main);

    var metrics = Resources.DisplayMetrics;
    var widthInDp = ConvertPixelsToDp(metrics.WidthPixels);
    var heightInDp = ConvertPixelsToDp(metrics.HeightPixels);

    FindViewById<TextView>(Resource.Id.screenWidthDp).Text = "Screen Width: " + widthInDp + " dp.";
    FindViewById<TextView>(Resource.Id.screenHeightDp).Text = "Screen Height: " + heightInDp + " dp.";
}
  1. After `OnCreate`, add the following helper to convert the pixels into density-independent pixels:
private int ConvertPixelsToDp(float pixelValue)
{
    var dp = (int) ((pixelValue)/Resources.DisplayMetrics.Density);
    return dp;
}
  1. Run the application. Depending on the device, it will display the screen height and width. The following screen shot is from a Nexus 4:

Additional Information

The structure DisplayMetrics contains general information about a device’s display. DisplayMetrics.Width and DisplayMetrics.Height will return the width and height of a screen in pixels. The appearance of a user interface is influenced by not only by the resolution in pixels, but the physical screen size and the density of the pixels. To help deal with this complexity, Android has a virtual unit of measurement called density-independent pixels (dp). Using density-independent pixels allows an Android application to render elements of the UI so that they appear the same on all screens.

DisplayMetrics.Density is a scaling factor that can be used to convert from pixels to density-independent pixels.

Xamarin Workbook

If it's not already installed, install the Xamarin Workbooks app first. The workbook file should download automatically, but if it doesn't, just click to start the workbook download manually.