3

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?

warvariuc
  • 57,116
  • 41
  • 173
  • 227
Nirav Dabhi
  • 341
  • 1
  • 7
  • 29
  • Check http://stackoverflow.com/questions/11330363/how-to-detect-device-is-android-phone-or-android-tablet – sdabet Feb 10 '14 at 06:07

5 Answers5

2

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?

Community
  • 1
  • 1
Jorgesys
  • 124,308
  • 23
  • 334
  • 268
  • 1
    Is there any need to change in XML layout file dear? – Nirav Dabhi Feb 10 '14 at 06:28
  • I have used the method using xml layouts or storing a variable in resources but that will be incorrect, every day we have mobile phone devices with different resolutions and this cannot be accomplished for certain devices. – Jorgesys Feb 10 '14 at 06:33
  • 1
    Yup I am facing the same problem. But my requirement is my app should run on mobile screen and tablet screen without overlapping. Is there any other soulution? – Nirav Dabhi Feb 10 '14 at 06:37
2

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.

J Jiang
  • 1,452
  • 10
  • 14
  • I had already done it. bt i dont know how to findout that screen is large or small – Nirav Dabhi Feb 10 '14 at 06:19
  • If you do care about if the screen size is small (default is not enough), you can create a new layout folder called `layout-small` – J Jiang Feb 10 '14 at 06:23
  • 1
    I had already done the same. the problem is that how to fetch the size of the screen in the coading/ – Nirav Dabhi Feb 10 '14 at 06:26
  • 1
    no need for the screen size. i want that my application run on the medium and large screen both. – Nirav Dabhi Feb 10 '14 at 06:27
  • @NiravDabhi Medium? You mean 7 inch tablets or the phones? – Glenn Feb 10 '14 at 06:34
  • Finally, I get your question.If you want to know this programmatically, I thinke @Glenn's answer quite matches your need. – J Jiang Feb 10 '14 at 06:35
  • ya i think so. ya and android phone doesn't have small screens i think. so normal screen is not the problem if i use scroll view. but the problem is in large screen. – Nirav Dabhi Feb 10 '14 at 06:41
2

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);
Glenn
  • 12,741
  • 6
  • 47
  • 48
1

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

Community
  • 1
  • 1
jmarcosSF
  • 69
  • 1
  • 8
  • First i had create the design which is suitable for mobile screen then i test that on tablet which is not suitable. so i create four design of the application. – Nirav Dabhi Feb 10 '14 at 06:22
  • Ok, I understand. If you need to create more of a responsive application, I'd look into implementing a "web view" app: http://www.mkyong.com/android/android-webview-example/ This is more or less how it's done professionally nowadays. Your android app will point to a URL, load it, and it can be interactive with your native android application via Javascript. Then, you can use Javascript libraries such as Twitter-Bootstrap to handle responsiveness based on the resolution of your browser. I don't know if this may help for your current implementation, but you may eventually need to do this. – jmarcosSF Feb 10 '14 at 06:33
  • Web View is not the solution sir. its best option when we load the data from URL. but how to implement when we have numbers of widgets. – Nirav Dabhi Feb 10 '14 at 06:38
  • Ok sir. i am trying as u suggested it will need major changes in my app. – Nirav Dabhi Feb 10 '14 at 06:39
  • I understand it may not be the best solution for what you are attempting to accomplish. But the pros of the suggested implementation is that it is platform-agnostic, and you can make changes to your styling and CSS based on whether you app is web-based, Android, Windows or iOS based. And although your app may need some major changes, the suggested solution may really help in the long run in regards to scalability and re-useability. Cons is that you may have to depend on cached data if there is no connection available to your server. Regardless, I hope this info helps one way or another. – jmarcosSF Feb 10 '14 at 06:45
  • Ok sir. I think so. But mostly my app doesnt need web view. Its all about web services. – Nirav Dabhi Feb 10 '14 at 06:48
0
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.

Joel
  • 838
  • 5
  • 12