31

My case is that the logic is same for both Phone and Tablet. But there is slight difference in the layout. And I tried with the following code

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

Samsung Tab 10" has the resolution of 1280 * 800 and S3 has the resolution of 1270 * 720. And this code returns the Size as XLarge for both the Tab and Phone as its criteria for checking is > 960 * 720.

I have tested inserting the respective UI in the layout folder in Res as Layout, Layout-Large and Layout-xLarge . But this didn't effect in anyway. while checking it took the UI from the Layout folder.

Anyway even though I place the UI in the different layout folders, I have to check them in the class file to set the respective ContentView.

Is there any other way to find it?

Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
VIGNESH
  • 2,023
  • 8
  • 31
  • 46
  • 1
    Check: [this](http://stackoverflow.com/questions/7251131/how-to-determine-the-target-device-programmatically-in-android), [this](http://stackoverflow.com/questions/9884517/how-to-know-whether-its-a-tablet-or-phone-in-android-programmatically), [this](http://stackoverflow.com/questions/11330363/how-to-detect-device-is-android-phone-or-android-tablet), [this](http://www.androidsnippets.com/how-to-detect-tablet-device) – Paresh Mayani May 28 '13 at 05:13
  • possible duplicate of [Determine if the device is a smartphone or tablet?](http://stackoverflow.com/questions/9279111/determine-if-the-device-is-a-smartphone-or-tablet) – Mario Kutlev May 12 '14 at 12:22

10 Answers10

64

This subject is discussed in the Android Training:

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

Here is implementation,

Credit goes to ol_v_er for this simple and easy approach.

Some additional Information

You have now flag indicate whether your application is running on phone or tablet.

I have created two packages to handle UI and it's functionality,

com.phone
com.tablet

And you redirect control to your needed package

boolean tabletSize = getResources().getBoolean(R.bool.isTablet);
if (tabletSize) {
    // do something
    //Start activity for tablet
} else {
    // do something else
    //Start activity for phone     
}

Refer

Note :I think for both 10 inch and 7 inch screen app take resources from res/values-sw600dp/ . But To be more specific I think for 10 inch tablet screen we can use res/values-sw720dp/

<resources>
    <bool name="isTablet">true</bool>
</resources>
edwin
  • 7,985
  • 10
  • 51
  • 82
17

Try this

    public boolean isTablet(Context context) {
        boolean xlarge = ((context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == 4);
        boolean large = ((context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_LARGE);
        return (xlarge || large);
    }

It will return true if you are using a tablet. It has been checked on Samsung Galaxy Tab 7" and Samsung Galaxy S3.

user1721904
  • 843
  • 4
  • 13
  • 10
    checking on two devices _IS NOT_ a good enough check. Be very wary of this method as it is not reliable. – stevebot Nov 24 '14 at 19:37
  • 2
    basing yourself on device dimension is the worst idea possible to determine a device. I don't know why this is the correct answer. – Jorel Amthor Feb 20 '18 at 20:50
3

For example, you could set some res-values folder:

res/values-xlarge res/values-large res/values-sw600dp

etc. Then You could declare a boolean for each one:

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

or

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

you can get the value by

   boolean xlargeValue = getResources().getBoolean(R.bool.isXlarge);
   boolean largevalue = getResources().getBoolean(R.bool.isLarge);
   boolean tabletValue = getResources().getBoolean(R.bool.sw620dp):
Opiatefuchs
  • 9,800
  • 2
  • 36
  • 49
3

Try this code your app is working device phone or tablet easy to fine call the method oncreate() inside

isTabletDevice(activity)

private static boolean isTabletDevice(Context activityContext) {

    boolean device_large = ((activityContext.getResources().getConfiguration().screenLayout &
          Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_LARGE)
    DisplayMetrics metrics = new DisplayMetrics();
    Activity activity = (Activity) activityContext;
    activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
    if (device_large) {
        //Tablet
        if (metrics.densityDpi == DisplayMetrics.DENSITY_DEFAULT){              
            return true;
        }else if(metrics.densityDpi == DisplayMetrics.DENSITY_MEDIUM){
            return true;    
        }else if(metrics.densityDpi == DisplayMetrics.DENSITY_TV){
            return true;    
        }else if(metrics.densityDpi == DisplayMetrics.DENSITY_HIGH){
            return true;    
        }else if(metrics.densityDpi == DisplayMetrics.DENSITY_280){
            return true;    
        }else if(metrics.densityDpi == DisplayMetrics.DENSITY_XHIGH) {
            return true;
        }else if(metrics.densityDpi == DisplayMetrics.DENSITY_400) {
            return true;
        }else if(metrics.densityDpi == DisplayMetrics.DENSITY_XXHIGH) {
            return true;
        }else if(metrics.densityDpi == DisplayMetrics.DENSITY_560) {
            return true;
        }else if(metrics.densityDpi == DisplayMetrics.DENSITY_XXXHIGH) {
            return true;
        }            
    }else{
        //Mobile
         } 
    return false;
}
Community
  • 1
  • 1
Satheeshkumar Somu
  • 496
  • 1
  • 7
  • 14
2

Old question, but this might help someone. If you want to find out if device is tablet (screen lager than 7"), or phone, you can use this util method:

Kotlin

fun isTablet(): Boolean {
    return App.instance.resources.configuration.smallestScreenWidthDp >= 600
}

Java

public static Boolean isTablet(){
    return App.instance.resources.configuration.smallestScreenWidthDp >= 600
}

App.instance is aplication instance.

Dimmy3
  • 926
  • 2
  • 13
  • 25
  • These values do not represent the physical size of the device. In multi-window mode, they will become smaller. – Miloš Černilovský Apr 17 '20 at 12:45
  • The Java code is mis-quoted. To start with, it is not Java code. To the person who posted it: if you want share any code which you didn't write, please understand it thoroughly, before rushing it out. I also agree with " Miloš Černilovský' comment: these values do not represent the physical size. The solution is definitely wrong in multi-window mode – somard Sep 29 '21 at 17:18
2
public boolean isTablet() { 
try { 
    // Compute screen size 
 Context context = this;
    DisplayMetrics dm = 
 context.getResources().getDisplayMetrics(); 
    float screenWidth  = dm.widthPixels / dm.xdpi; 
    float screenHeight = dm.heightPixels / dm.ydpi; 
    double size = Math.sqrt(Math.pow(screenWidth, 2) + 
                            Math.pow(screenHeight, 2)); 
    // Tablet devices have a screen size greater than 6 
      inches 
    return size >= 6; 
  } catch(Throwable t) { 
    Log.e("Failed to compute screen size", t.toString()); 
    return false; 
  } 

}

1

Try this code. You can get the screen inches, On the basis of size you can get the tablet or android device

 String inputSystem;
    inputSystem = android.os.Build.ID;
    Log.d("hai",inputSystem);
    Display display = getWindowManager().getDefaultDisplay(); 
    int width = display.getWidth();  // deprecated
    int height = display.getHeight();  // deprecated
    Log.d("hai",width+"");
    Log.d("hai",height+"");
    DisplayMetrics dm = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(dm);
    double x = Math.pow(width/dm.xdpi,2);
    double y = Math.pow(height/dm.ydpi,2);
    double screenInches = Math.sqrt(x+y);
    Log.d("hai","Screen inches : " + screenInches+"");
Sunil Kumar
  • 7,086
  • 4
  • 32
  • 50
  • Yes but if there are only these two devices ( Say samsung S3 and Samsung 10" Tablet) we can compare , but how do i do for other device also ....... there are various resolution sizes for various devices . S4 device screen is even more bigger . – VIGNESH May 28 '13 at 05:17
  • but android device size is not like tablet size, so you can only get this one is android mobile device or tablet. – Sunil Kumar May 28 '13 at 05:22
1

Use different resource files rather than trying to determine it programmatically. This will be enough for the majority of cases and is what the documentation recommends.

enter image description here

See my fuller answer here.

Community
  • 1
  • 1
Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
0

All other questions use resource qualifiers and methods, which do not represent the PHYSICAL size of the device, but the AVAILABLE screen size. For example, in the multi-window mode, the system will get resources from the "values" folder instead of "values-large", because the available screen size for the app became smaller. To determine, whether the physical device is a tablet or phone, use the following method (I use 640x480dp as the minimum size for a tablet, which is the definition of large devices, feel free to change these constants):

fun isTablet(context: Context): Boolean {
    val outSize = Point()
    val windowManager = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager
    windowManager.defaultDisplay.getRealSize(outSize)
    outSize.x = pxToDp(windowManager, outSize.x)
    outSize.y = pxToDp(windowManager, outSize.y)
    val shorterSideDp: Int
    val longerSideDp: Int
    if (outSize.x > outSize.y) {
        shorterSideDp = outSize.y
        longerSideDp = outSize.x
    } else {
        shorterSideDp = outSize.x
        longerSideDp = outSize.y
    }
    return shorterSideDp > 480 && longerSideDp > 640
}

Function for converting PX to DP:

@Dimension(unit = Dimension.DP)
fun pxToDp(windowManager: WindowManager, @Dimension(unit = Dimension.PX) px: Int): Int {
    val displayMetrics = DisplayMetrics()
    windowManager.defaultDisplay.getRealMetrics(displayMetrics)
    return (px / displayMetrics.densityDpi.toFloat() * DisplayMetrics.DENSITY_DEFAULT).roundToInt()
}
Miloš Černilovský
  • 3,846
  • 1
  • 28
  • 30
-1

This is working perfectly well in my app:

private boolean isPhoneDevice(){

    return getPackageManager().hasSystemFeature(PackageManager.FEATURE_TELEPHONY);

}
user2288580
  • 2,210
  • 23
  • 16
  • 3
    This only checks if the device can work as a telephone. Definition: "The device has a telephony radio with data communication support.". Some tablets and phones with really big screns will return true on this. – Andreas Evjenth Oct 10 '16 at 08:01