Reputation: 343
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
login_portrait.xml
login_landscape.xml
login_portrait_large.xml
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
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
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:
They can be overlapped sometime, read this for more info.
Upvotes: 2
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
Reputation: 12819
You can put XML (bools.xml) files in the following folders
values-large
values-xlarge
values-normal
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
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