How to set activity orientation in Android based device?

I have a situation where the Android device is a tablet, then I want the orientation to be on (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 it?

Thanks Nithin

+6
source share
5 answers

You have 2 methods:

a) Create different sets of layouts and drawings for the phone and tablet separately and design them according to the necessary orientation.

b) Check the working device at run time and set the orientation. To check the device, check the following settings and programmatically adjust the orientation,

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

try this It works great for me .....

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

and this function allows you to choose between the tablet and the phones called in the inverter above .....

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

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

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

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

get the width and height of the deviceโ€™s screen, if the resolution is less than that of the tablet, then you can adjust the orientation of the application to the portrait.

If you need display sizes in activity pixels, you can use getSize:

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

If there is no Activity, you can get the default mapping 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 ); } 

Do not specify a command in a manifest based

Android: screenOrientation

0
source

I'm just a beginner, but maybe this can be done by focusing on a certain level of API or dpi? You can provide alternative inventory for the different devices youโ€™re targeting.

For instance:

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

Good documentation for this can be found here .

0
source

Source: https://habr.com/ru/post/908306/


All Articles