Nirav Dabhi
Nirav Dabhi

Reputation: 343

How to find out if application is running on tablet or mobile device in android?

I am developing an android application which is compatible for mobile screen and tablet screen both.

So I am creating 4 different screens for each. That is

  1. login_portrait.xml
  2. login_landscape.xml
  3. login_portrait_large.xml
  4. login_landscape.xml

But the problem is that how to find that my application used by any user is using tablet or mobile?

Is there any solution?

Upvotes: 3

Views: 5014

Answers (5)

Joel
Joel

Reputation: 838

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

You can also have it in a Base class and make it protected that way you can reuse it in all the activities that extend your base class.

Upvotes: 0

J Jiang
J Jiang

Reputation: 1512

You can achieved this by Using configuration qualifiers. Put the different .xml layout files in the following res folders specifically and android will do it for you.

res/layout-small/login.xml             // login layout for small portrait
res/layout-normal/login.xml             // login layout for normal portrait
res/layout-normal-land/login.xml       // login layout for normal land
res/layout-large/login.xml        // login layout for large portrait
res/layout-large-land/login.xml    // login layout for large land

You can just use the login.xml and android will choose the proper one for you.

Note. for android 3.2 or higher, a new configuration qualifiers system is used. You can read more about it from the official develop guide.

EDIT: BTW, each screen size is defined as:

  • xlarge screens are at least 960dp x 720dp
  • large screens are at least 640dp x 480dp
  • normal screens are at least 470dp x 320dp
  • small screens are at least 426dp x 320dp

They can be overlapped sometime, read this for more info.

Upvotes: 2

Jorgesys
Jorgesys

Reputation: 126563

I use this method:

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

and it will be enough, if returns false then your device is a mobile phone.

More info: How to detect device is Android phone or Android tablet?

Upvotes: 2

Glenn
Glenn

Reputation: 12819

You can put XML (bools.xml) files in the following folders

  1. values-large
  2. values-xlarge
  3. values-normal
  4. values-small

For values-large/bools.xml and values-xlarge/bools.xml

<bool name="tablet">true</bool>

For values-normal/bools.xml and values-small/bools.xml

<bool name="tablet">false</bool>

Then to determine programmatically,

boolean isTablet = context.getResources().getBoolean(R.bool.tablet);

Upvotes: 2

jmarcosSF
jmarcosSF

Reputation: 69

You may need to include a bit more info in your question.

Are you collecting data dependent on whether it's a mobile or tablet? If so, you may want to use Javascript to detect the resolutionstack overflow question

That, or you can just detect the "user-agent".

If you need to make the application a bit more responsive contingent on the browser-type, than try twitter-bootstrap: twitter-bootstrap

Upvotes: 1

Related Questions