Lokesh
Lokesh

Reputation: 5378

Detecting whether Android device is a phone or a tablet using code

I did one application for both mobile and Tablet. Here I want visible few of my contents on Mobile but not in Tablet.

How can I Detect whether Android device is a phone or a tablet while launching my activity.

I Google and find out few suggestions like User -agent.. How can I use it or any other solutions??

Thanks in advance...

Upvotes: 0

Views: 1637

Answers (4)

gunar
gunar

Reputation: 14720

Well, when it comes to using different resources for different types of screen sizes, the best article is "Supporting Multiple Screens".

My easiest way of detecting device type is to put different values in strings.xml for a single key, let's call it "device_type". So in:

  1. values -> strings.xml I will have <string name="device_type">smartphone</string>
  2. values-large -> strings.xml I will have <string name="device_type">tablet</string>
  3. values-small -> strings.xml I will have <string name="device_type">small</string>
  4. values-xlarge -> strings.xml I will have <string name="device_type">xlarge</string>

Having a context, I will read this value and determine what type of device I have.

Upvotes: 1

Arun C
Arun C

Reputation: 9035

Use

public static boolean isTablet(Context context) {
    return hasHoneycomb() && isTab(context);
}

With supporting functions

     public static boolean hasHoneycomb() {
        return Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB;
    }

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

Upvotes: 0

Addicted Manish
Addicted Manish

Reputation: 189

public static boolean isTablet (Context context) 
{

    return (context.getResources().getConfiguration().screenLayout
            & Configuration.SCREENLAYOUT_SIZE_MASK)
            >= Configuration.SCREENLAYOUT_SIZE_LARGE;
}

this will returne the true if the tablet or false if the mobile

Upvotes: 1

vbod
vbod

Reputation: 58

TelephonyManager manager = (TelephonyManager) getApplicationContext().getSystemService(Context.TELEPHONY_SERVICE);

if (manager.getPhoneType() == TelephonyManager.PHONE_TYPE_NONE)
{
     //It's a tablet
}
else
{
     //It's a phone
}

It should works

Upvotes: 1

Related Questions