Bart Friederichs Bart Friederichs - 17 days ago 4
Android Question

Have a fixed orientation based on screen size

I have an Android app built on a phone that I am going to update now to also support tablet-sized screens (in particular Galaxy Tab 10.1). I figured out the whole

res/layout
thing, so that's fine. However, I built my app to have a fixed screen orientation in
AndroidManifest.xml
, set as
android:screenOrientation="portrait"
.

Now, I want to have the following: have a fixed orientation for each screen size, the one fixed in portrait (layout-small) and the other as landscape.

Using
res/layout-small-land
and
res/layout-large-port
doesn't do what I want, because it still switches from portrait to landscape and back. Worse even, the app crashes when I rotate my phone to landscape, because the
res/layout-small-land
doesn't exist.

Can I do this by just defining XML files, or do I have to add code?

Answer

Instead of fixing the orientation on the manifest, you could use setRequestedOrientation. On the onCreate method of each activity, check if you are on a tablet or smartphone and set the desired orientation.

Another option could be Creating Multiple APKs for Different Screen Sizes, depending on your needs.