Detect Screen Size
-
Upload
iman-soleimany-zadeh -
Category
Documents
-
view
22 -
download
4
description
Transcript of Detect Screen Size
![Page 1: Detect Screen Size](https://reader031.fdocuments.in/reader031/viewer/2022020122/552e043a4a7959195e8b48d9/html5/thumbnails/1.jpg)
DETECT THE SCREEN SIZE
This recipe will show how to detect the screen size of a device, in density-
independent pixels.
Sample Code:Sample Code:Sample Code:Sample Code:
[DetectScreenSize.zip]
Related Xamarin Documentation:Related Xamarin Documentation:Related Xamarin Documentation:Related Xamarin Documentation:
Creating Resources for Varying Screens
Related Related Related Related GoogleGoogleGoogleGoogle Documentation:Documentation:Documentation:Documentation:
Supporting Multiple Screens
DisplayMetrics
Recipe
1. Create a new Mono for Android application named ScreenSize.
![Page 2: Detect Screen Size](https://reader031.fdocuments.in/reader031/viewer/2022020122/552e043a4a7959195e8b48d9/html5/thumbnails/2.jpg)
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>
3. 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.";
}
4. After OnCreate, add the following helper to:
private int ConvertPixelsToDp(float pixelValue)
{
var dp = (int) ((pixelValue)/Resources.DisplayMetrics.Density);
return dp;
}
5. Run the application. Depending on the device, it will display the screen height and width. The following screen shot is from a Galaxy Nexus:
![Page 3: Detect Screen Size](https://reader031.fdocuments.in/reader031/viewer/2022020122/552e043a4a7959195e8b48d9/html5/thumbnails/3.jpg)
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.