简体   繁体   中英

Is it possible to Force mobile to use vertical layout while tablet to use horizontal layout in single app?

I have an application that supports all Android devices.

I want to know is it possible, based on device:

  • if device is phone/phablet then regardless of screen is in portrate mode or landscape mode then application uses vertical layout
  • if device is tablet then regardless of screen is in portrate mode or landscape mode then application uses landscape layout

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM