Reputation: 5378
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
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:
strings.xml
I will have <string name="device_type">smartphone</string>
strings.xml
I will have <string name="device_type">tablet</string>
strings.xml
I will have <string name="device_type">small</string>
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
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
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
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