0

I want to disable changing orientation in my android app, but I want to allow user use his native orientation: landscape for tablets and portrait for phones.

I try to set "android:screenOrientation" with different values, but I can't find appropriate value.

Is such task needs to write java code (and what code I need to write), or this can be solved using manifest?

UPD As in @Pierrew's hint, I need to create res/layout/main.xml for default layout, res/layout-land/main.xml for landscape layout and res/layout-port/main.xml for portrait layout. So, I need to know, how to defined all layout in res/layout/main.xml and only clarify orientation in res/layout-land/main.xml and res/layout-port/main.xml.

I think I need to declare layout in res/layout/main.xml, and derive all layout properties in res/layout-land/main.xml and res/layout-port/main.xml and override only android:screenOrientation value in derived layout.

How I can do it?

Bernd Jacobi
  • 571
  • 2
  • 5
  • 18
  • 1
    That question was answered [here](http://stackoverflow.com/questions/9627774/android-allow-portrait-and-landscape-for-tablets-but-force-portrait-on-phone) – Víctor Albertos Aug 29 '14 at 21:22

2 Answers2

0

setting android:screenOrientation="landscape" in the layout xml is the way to do it. To support different devices (tablet and phones), you need to use Size-Qualifiers. In your res folder, you will have to create a res/layout-large/main.xml to support tablet. So if you want landscape for tablets. In your res/layout-large/main.xml, you put android:screenOrientation="landscape". In your normal res/layout/main.xml, you can put android:screenOrientation="portrait". Android will automatically detect what device the user is using and load the appropriate xml. For further information, check out the size qualifier section in http://developer.android.com/training/multiscreen/screensizes.html#TaskUseSizeQuali

Pierrew
  • 462
  • 5
  • 15
  • And how I can describe layout in one file, and describe only the differences in res/layout-large/main.xml (and what I need to create for phone?) – Bernd Jacobi Aug 29 '14 at 22:48
  • If everything else stays the same, then you will have to copy everything inside res/layout (by default this folder is for phone) to res/layout-large/ and then make your change in configuration. That's by Android design because people usually rearrange the layout to make sure everything fit in the larger screen. – Pierrew Aug 29 '14 at 23:02
  • And also, if you use relativelayout, all the UI elements may be shifted, you will have to make sure everything is aligned properly when you switch from landscape mode to portrait mode. – Pierrew Aug 29 '14 at 23:08
0

You can use a little logic using these methods.

Boolean isTablet = (ctx.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_LARGE;

Change Screen Orientation programatically.

ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
ActivityInfo.SCREEN_ORIENTATION_PORTRAIT

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

ActivityInfo:

http://developer.android.com/reference/android/content/pm/ActivityInfo.html

This should do the trick for you:

import android.provider.Settings;
public static void setAutoOrientationEnabled(ContentResolver resolver, boolean enabled){
   Settings.System.putInt( context.getContentResolver(), Settings.System.ACCELEROMETER_ROTATION, enabled ? 1 : 0);
}

Add permission to the AndroidManifest.xml

<uses-permission android:name="android.permission.WRITE_SETTINGS" />
Javier
  • 23
  • 2
  • 9