How to properly detect if an Android device is a phone or a tablet?

dlpopescuCAdlpopescuCA USMember ✭✭
edited November 2017 in Xamarin.Android

Hi, all.

Up until now, I was using the ScreenLayout to detect if an Android device is a phone or a tablet, to force the orientation to Portrait/Landscape. The problem with the code below is that, for devices such as the Google Pixel 2, it returns true for SizeLarge. Is there any bulletproof way to detect if a device can be classified as a tablet? I was thinking about using a ratio between the physical size of the screen and the DPI, but I feel that it still is prone to errors.
Thank you.

context = context ?? MainApplication.Context;
var screenLayout = context.Resources.Configuration.ScreenLayout & Android.Content.Res.ScreenLayout.SizeMask;
return screenLayout == Android.Content.Res.ScreenLayout.SizeLarge || screenLayout == Android.Content.Res.ScreenLayout.SizeXlarge;

Best Answers

  • dlpopescuCAdlpopescuCA US ✭✭
    Accepted Answer

    @SergeyIvanov.9607 : while your method should work, it is only usable in Xamarin.Forms app. My bad for not specifying I am NOT using Xamarin.Forms.
    @Charwaka : thank you, but it is not what I was looking for.

    In case someone else is interested, I have settled to this piece of code, which works wonders:

    public static bool IsTablet(this Context context)
        {
            Display display = ((Activity)context).WindowManager.DefaultDisplay;
            DisplayMetrics displayMetrics = new DisplayMetrics();
            display.GetMetrics(displayMetrics);
    
            var wInches = displayMetrics.WidthPixels / (double)displayMetrics.DensityDpi;
            var hInches = displayMetrics.HeightPixels / (double)displayMetrics.DensityDpi;
    
            double screenDiagonal = Math.Sqrt(Math.Pow(wInches, 2) + Math.Pow(hInches, 2));
            return (screenDiagonal >= 7.0);
        }
    

Answers

  • CharwakaCharwaka INMember ✭✭✭✭

    ****Hi @dlpopescuCA

    use this code

     TelephonyManager manager = this.GetSystemService(Context.TelephonyService) as TelephonyManager;
                if (manager.PhoneType == PhoneType.None)
                {
                    System.Diagnostics.Debug.WriteLine("Tablet");
                }
                else
                {
                    System.Diagnostics.Debug.WriteLine("Mobile");
                }
    

    Don't forget to mark as answer

  • dlpopescuCAdlpopescuCA USMember ✭✭

    Thank you, @Charwaka . Unfortunately, there are tablets with 3G/4G capabilities, so this code is not 100% reliable.

  • dlpopescuCAdlpopescuCA USMember ✭✭

    Thanks, @Charwaka , I've already been through all forums, apparently there is no 100% reliable way to detect the device type on Android.

  • dlpopescuCAdlpopescuCA USMember ✭✭
    Accepted Answer

    @SergeyIvanov.9607 : while your method should work, it is only usable in Xamarin.Forms app. My bad for not specifying I am NOT using Xamarin.Forms.
    @Charwaka : thank you, but it is not what I was looking for.

    In case someone else is interested, I have settled to this piece of code, which works wonders:

    public static bool IsTablet(this Context context)
        {
            Display display = ((Activity)context).WindowManager.DefaultDisplay;
            DisplayMetrics displayMetrics = new DisplayMetrics();
            display.GetMetrics(displayMetrics);
    
            var wInches = displayMetrics.WidthPixels / (double)displayMetrics.DensityDpi;
            var hInches = displayMetrics.HeightPixels / (double)displayMetrics.DensityDpi;
    
            double screenDiagonal = Math.Sqrt(Math.Pow(wInches, 2) + Math.Pow(hInches, 2));
            return (screenDiagonal >= 7.0);
        }
    
Sign In or Register to comment.