Spotlight
Spotlight

Reputation: 1299

Correctly detect Android device type

Is there any a way to precisely detect the device type (phone, tablet, watch, TV, auto, PC)?

Right now, I found a way to detect if the app is running on a car (uiModeManager.getCurrentModeType() == Configuration.UI_MODE_TYPE_CAR), on a TV (uiModeManager.getCurrentModeType() == Configuration.UI_MODE_TYPE_TELEVISION), or on a watch (uiModeManager.getCurrentModeType() == Configuration.UI_MODE_TYPE_WATCH). Is it correct? Does a phone connected to a car appears as a phone or as "Android Auto"?

To differentiate between a phone, tablet or computer I can check for the minimum screen size (600dp to qualify as tablet or laptop for example).

The problem now is to differentiate between a tablet and a laptop. Have you got any idea?

PS: I'm not asking this to make a responsive UI, it's a question related to the device management for an account

Upvotes: 27

Views: 22668

Answers (6)

Magesh Pandian
Magesh Pandian

Reputation: 9369

Check whether the physical screen size is large (=tablet or laptop-like device):

private static boolean isTabletDevice(Context activityContext) {
    boolean device_large = ((activityContext.getResources().getConfiguration().screenLayout &
            Configuration.SCREENLAYOUT_SIZE_MASK) ==
            Configuration.SCREENLAYOUT_SIZE_LARGE);

    if (device_large) {
        DisplayMetrics metrics = new DisplayMetrics();
        Activity activity = (Activity) activityContext;
        activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);

        if (metrics.densityDpi == DisplayMetrics.DENSITY_DEFAULT
                || metrics.densityDpi == DisplayMetrics.DENSITY_HIGH
                || metrics.densityDpi == DisplayMetrics.DENSITY_MEDIUM
                || metrics.densityDpi == DisplayMetrics.DENSITY_TV
                || metrics.densityDpi == DisplayMetrics.DENSITY_XHIGH) {
            AppInstance.getLogger().logD("DeviceHelper","IsTabletDevice-True");
            return true;
        }
    }
    AppInstance.getLogger().logD("DeviceHelper","IsTabletDevice-False");
    return false;
}

Check whether the app is running in the Android SDK emulator:

public static boolean isEmulator() {
return Build.FINGERPRINT.startsWith("generic")
        || Build.FINGERPRINT.startsWith("unknown")
        || Build.MODEL.contains("google_sdk")
        || Build.MODEL.contains("Emulator")
        || Build.MODEL.contains("Android SDK built for x86")
        || Build.MANUFACTURER.contains("Genymotion")
        || (Build.BRAND.startsWith("generic") && Build.DEVICE.startsWith("generic"))
        || "google_sdk".equals(Build.PRODUCT);

}

Check whether the device is an Android TV:

    public static final String TAG = "DeviceTypeRuntimeCheck";

    UiModeManager uiModeManager = (UiModeManager) getSystemService(UI_MODE_SERVICE);
    if (uiModeManager.getCurrentModeType() == Configuration.UI_MODE_TYPE_TELEVISION) {
        Log.d(TAG, "Running on a TV Device")
    } else {
        Log.d(TAG, "Running on a non-TV Device")
}

Upvotes: 2

Jijo
Jijo

Reputation: 520

please refer this link,

http://developer.android.com/training/multiscreen/screensizes.html#TaskUseSWQuali

here below i put the code for checking Tablet or android TV, please check, it will works

for tablet

private boolean checkIsTablet() {
   boolean isTablet;
   Display display = ((Activity)   this.mContext).getWindowManager().getDefaultDisplay();
   DisplayMetrics metrics = new DisplayMetrics();
   display.getMetrics(metrics);

  float widthInches = metrics.widthPixels / metrics.xdpi;
  float heightInches = metrics.heightPixels / metrics.ydpi;
  double diagonalInches = Math.sqrt(Math.pow(widthInches, 2) + Math.pow(heightInches, 2));
  if (diagonalInches >= 7.0) {
    isTablet = true;
  }

return isTablet;
}

or

public static boolean checkIsTablet(Context ctx){    
return (ctx.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_LARGE; 
}

for TV

@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
private boolean checkIsTelevision() {
  boolean isAndroidTV;
  int uiMode = mContext.getResources().getConfiguration().uiMode;
  if ((uiMode & Configuration.UI_MODE_TYPE_MASK) == Configuration.UI_MODE_TYPE_TELEVISION) {
    isAndroidTV = true;
}

It will works, enjoy...

Upvotes: 5

MIkka Marmik
MIkka Marmik

Reputation: 1102

You can detect using this code application running on large screen or not.

public static boolean isTablet(Context context) {
    return (context.getResources().getConfiguration().screenLayout
            & Configuration.SCREENLAYOUT_SIZE_MASK)
            >= Configuration.SCREENLAYOUT_SIZE_LARGE;
  }

This link would be also helpful to you.

Get Width of screen and check that with this break-points.

/* Tablet (portrait and landscape) ----------- */

 min-device-width : 768px
 max-device-width : 1024px

/* Desktops and laptops ----------- */

min-width : 1224px

Upvotes: 14

A. Badakhshan
A. Badakhshan

Reputation: 1043

To differentiate between a phone and a tablet or computer I can check for the minimum screen size (600dp to qualify as talet or laptop for example).

There is a better way to do that and it's using values. For example, if you have 2 type of devices (say phone and tablet), create two folder for values too. Then for values folder add this:

<resources>
    <bool name="isLarge">false</bool>
</resources>

and in your values-large folder:

<resources>
    <bool name="isLarge">true</bool>
</resources>

Then in your activity:

boolean isLarge = getResources().getBoolean(R.bool.isLarge);
if (isLarge) {
    // do something
} else {
    // do something else
}

Using this, you can do same thing for phone, sw-600dp, sw-720dp and etc. I'm not sure if you can use this for TV and etc, but I think it worth to try.

Upvotes: 5

Multidots Solutions
Multidots Solutions

Reputation: 591

Use following static methods for getting device name,id, height, and width. For more detail visit at this link. Project includes all common feature that basic android application want.

     public static String getDeviceName() {
        String manufacturer = Build.MANUFACTURER;
        String model = Build.MODEL;
        if (model.startsWith(manufacturer)) {
            return capitalize(model);
        } else {
            return capitalize(manufacturer) + " " + model;
        }
    }

    public static String getDeviceID(Context context) {
        TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        return telephonyManager.getDeviceId();
    }

    public static int getDeviceHeight(Context mContext) {
        DisplayMetrics displaymetrics = new DisplayMetrics();
        ((Activity) mContext).getWindowManager().getDefaultDisplay()
                .getMetrics(displaymetrics);
        return displaymetrics.heightPixels;
    }

    public static int getDeviceWidth(Context mContext) {
        DisplayMetrics displaymetrics = new DisplayMetrics();
        ((Activity) mContext).getWindowManager().getDefaultDisplay()
                .getMetrics(displaymetrics);
        return displaymetrics.widthPixels;
    }

Upvotes: 1

ARK
ARK

Reputation: 4054

Check this class. https://developer.android.com/reference/android/os/Build.html

String manufacturer = Build.MANUFACTURER
String model = Build.MODEL

You can know if its phone, tab or TV from Build.MODEL

Upvotes: 0

Related Questions