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);
        }
    
  • I was just trying to figure this out. Thanks for the great solution @dlpopescuCA

    I also agree that @Charwaka had a good solution in a very precise world. We are writing apps to run on android handheld computer devices such as those used in warehouses and trucking industry. They have a phone format but no phone capabilities. They run attached via WiFi to a Cellular Modem and Router in the vehicle.

  • oreis91oreis91 Member
    edited August 31

    Hello, I'm new here, I'm Brazilian and I would like to help.
    function UPDATE - 08/2018

    TelephonyManager manager = (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
    if (manager.getPhoneType() == manager.PHONE_TYPE_NONE) {
        /* TABLET */
        return true;
    }
    else {
        /* SMARTPHONE */
        return false;
    }
    
  • hvaughanhvaughan USMember ✭✭✭

    It is probably a safe bet to just do the same thing Xamarin Forms does to figure it out:

    https://github.com/xamarin/Xamarin.Forms/blob/d6dbccde98452796bdd7d8de60425a49db06c336/Xamarin.Forms.Platform.Android/Forms.cs#L184

Sign In or Register to comment.