Is there a way to determine Android physical screen size?

Is there a way to determine Android physical screen size?

We're currently porting our games to Android, and would like to be able to release a universal build for tablets and phones. Is there a way to determine the actual screen size or DPI in order to adjust the size of interface elements accordingly? It took a fair bit of research.
Here's what we have tried:
using UnityEngine;

public class DisplayMetricsAndroid {

 // The logical density of the display
 public static float Density { get; protected set; }

 // The screen density expressed as dots-per-inch
 public static int DensityDPI { get; protected set; }

 // The absolute height of the display in pixels
 public static int HeightPixels { get; protected set; }

 // The absolute width of the display in pixels
 public static int WidthPixels { get; protected set; }

 // A scaling factor for fonts displayed on the display
 public static float ScaledDensity { get; protected set; }

 // The exact physical pixels per inch of the screen in the X dimension
 public static float XDPI { get; protected set; }

 // The exact physical pixels per inch of the screen in the Y dimension
 public static float YDPI { get; protected set; }

 static DisplayMetricsAndroid() {
     // Early out if we're not on an Android device
     if (Application.platform != RuntimePlatform.Android) {
         return;
     }

     // The following is equivalent to this Java code:
     //
     // metricsInstance = new DisplayMetrics();
     // UnityPlayer.currentActivity.getWindowManager().getDefaultDisplay().getMetrics(metricsInstance);
     //
     // ... which is pretty much equivalent to the code on this page:
     // http://developer.android.com/reference/android/util/DisplayMetrics.html

     using (
         AndroidJavaClass unityPlayerClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer"),
         metricsClass = new AndroidJavaClass("android.util.DisplayMetrics")
     ) {
         using (
             AndroidJavaObject metricsInstance = new AndroidJavaObject("android.util.DisplayMetrics"),
             activityInstance = unityPlayerClass.GetStatic("currentActivity"),
             windowManagerInstance = activityInstance.Call("getWindowManager"),
             displayInstance = windowManagerInstance.Call("getDefaultDisplay")
         ) {
             displayInstance.Call("getMetrics", metricsInstance);
             Density = metricsInstance.Get("density");
             DensityDPI = metricsInstance.Get("densityDpi");
             HeightPixels = metricsInstance.Get("heightPixels");
             WidthPixels = metricsInstance.Get("widthPixels");
             ScaledDensity = metricsInstance.Get("scaledDensity");
             XDPI = metricsInstance.Get("xdpi");
             YDPI = metricsInstance.Get("ydpi");
         }
      }
   }
}

Solutions/Answers:

Answer 1:

I’m giving you how I did this in java before I switched to Unity. Perhaps you can use it in c# with small modifications:

So lately I did it this way:

screenWidth = getResources().getDisplayMetrics().widthPixels;
screenHeight = getResources().getDisplayMetrics().heightPixels;  

But I used to do it this way too (probably deprecated):

Display display = getWindowManager().getDefaultDisplay();
        if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.HONEYCOMB_MR2){
            android.graphics.Point screenSize = new android.graphics.Point();
            display.getSize(screenSize);
            screenWidth = screenSize.x;
            screenHeight = screenSize.y;
        }else{
            screenWidth = display.getWidth();
            screenHeight = display.getHeight();
        }

References