VIGNESH VIGNESH - 2 months ago 14
Android Question

How to find Tablet or Phone in Android , Programmatically?

I was searching for many days and i am not successful in finding a solution .

I have went through all the related threads and it was not helpful for me .

My case is that , 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 didnt 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 anyother way to find , Any related answers are welcomed .

Thanks in Advance .

Answer

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.