I have an application that supports all Android devices.
I want to know is it possible, based on device:
Therefore, application displays vertical on mobile phones (regardless of portrate or landscape mode) and displays horizontal on tablets (regardless of portrate mode or landscape mode)
By use of following lines of code application always displays portrait. Although it works for mobile but doesn't work for tablet.
android:screenOrientation = "portrait"
android:configChanges="keyboard|keyboardHidden|orientation"
I want to force application that uses vertical design if device is phone/phablet or uses horizontal design if device is tablet.
Hope my explanation is clear. Any suggestion would be appreciated. Thanks.
Yes you can know device is tablet or not..
Here is my code..
if(isTablet(this))
{
setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
If device is tablet, function will return true
public static boolean isTablet(Context context) {
return (context.getResources().getConfiguration().screenLayout
& Configuration.SCREENLAYOUT_SIZE_MASK)
>= Configuration.SCREENLAYOUT_SIZE_LARGE; }
i think you can try this way first get the screen size of the device
if ((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_LARGE) {
Toast.makeText(this, "Large screen",Toast.LENGTH_LONG).show();
}
else if ((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_NORMAL) {
Toast.makeText(this, "Normal sized screen" , Toast.LENGTH_LONG).show();
}
else if ((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_SMALL) {
Toast.makeText(this, "Small sized screen" , Toast.LENGTH_LONG).show();
}
else {
Toast.makeText(this, "Screen size is neither large, normal or small" , Toast.LENGTH_LONG).show();
}
and then set orientation like this
setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.