1

So I have such problem I want to set layout default orientation in AndroidManifest.xml as landscape, but have different orientation for some layouts as portrait, but when I add android:screenOrientation="portrait" it doesn't override manifest configuration is there any work around? Main idea is to have tablets layout forced landscape and phone layout forced portrait.

Here is an example of the same app for tablet and phone notice how layout orientation as landscape is forced for tablets and it doesn't rotate when device is rotated. And for phone is locked in portrait orientation and it doesn't rotate when device is rotated enter image description here

LeTadas
  • 3,436
  • 9
  • 33
  • 65
  • Yea I know, but it has to be done like this :) I updated my question, main Idea is to have different orientations for tablets and phones – LeTadas Dec 20 '18 at 11:56
  • 1
    explaining what you are trying to accomplish with a picture would be better for understanding your problem. – karan Dec 20 '18 at 11:57
  • you can android:screenOrientation on a single activity, not in the "application" tag. If you want to define both landscape and portait configuration you should follow this page of the documentation https://developer.android.com/training/multiscreen/screensizes – Nicola Gallazzi Dec 20 '18 at 12:01
  • I tried adding only for layout but then it doesn't force it. When I rotate the device it switches orientation – LeTadas Dec 20 '18 at 12:12
  • first you identify app install in tablet or mobile device. follow this link : https://stackoverflow.com/questions/11330363/how-to-detect-device-is-android-phone-or-android-tablet and then you paste code of orientation for tablet. @kosas – Ali Dec 20 '18 at 12:15
  • this is promising one – LeTadas Dec 20 '18 at 12:17
  • gonna wait for more answers would be cool to set it through layout only – LeTadas Dec 20 '18 at 12:17

1 Answers1

0

You can use setRequestedOrientation but landscape case the phone rotation must be enabled:

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

Calculate the screen size considering that tablet are more than 6.9" (I used this at my application) :

 DisplayMetrics dm = new DisplayMetrics();
 getWindowManager().getDefaultDisplay().getMetrics(dm);
 double x = Math.pow(dm.widthPixels / dm.xdpi, 2);
 double y = Math.pow(dm.heightPixels / dm.ydpi, 2);
 double screenInches = Math.sqrt(x + y);
Oussema Aroua
  • 5,225
  • 1
  • 24
  • 44