简体   繁体   中英

How do we set the orientation of an activity in Android, based on the device?

I have a situation, wherein, if the Android device is a tablet, then I want the orientation to be enabled (portrait and landscape), whereas if the device is a phone, I want the orientation to be only in Portrait mode. Can you help me how to do this?

Thanks, Nithin

try this It works perfectly for me.....

if ( isXLargeScreen() )
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

and this function to decide between tablet and phones,Called in the IF above.....

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

You have 2 methods :

a) Create different sets of layout and drawables for the phone and tablet separately and design them according to the orientation you need.

b) Check for the operating device during runtime and set the orientation. To check the device , give a check on the following parameters and set the orientation programatically,

  (1) Build.VERSION.SDK_INT
  (2) Configuration.SCREENLAYOUT_SIZE_MASK

You can use this class to get the device information about the current build, extracted from system properties.

To set the orientation properties, use following attribute...

android:screenOrientation=["unspecified" | "user" | "behind" |
                                     "landscape" | "portrait" |
                                     "reverseLandscape" | "reversePortrait" |
                                     "sensorLandscape" | "sensorPortrait" |
                                     "sensor" | "fullSensor" | "nosensor"]

get the width and height of the device screen, if the resolution is less than that of tablet, then you can set the orientation of the app to portrait.

If need the display dimensions in pixels of activity, you can use getSize:

Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;

If not an Activity you can get the default Display through WINDOW_SERVICE:

WindowManager wm = (WindowManager) ctx.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();

if(width<= (resolution width of tablet) && height<=(resolution height of tablet))
{
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT );
}

Dont give any command in manifest based on

android:screenOrientation

I'm just a beginner at this myself, but perhaps it'd be possible to do this by targeting a specific API level or dpi? You can provide alternate resources for the different devices you are targeting.

For example:

res/layout/main.xml
res/layout-xhdpi/main.xml
res/layout-v11/main.xml

A good documentation for that can be found here .

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