Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 433
Bart Friederichs Avatar asked Sep 01 '25 10:09

Bart Friederichs


1 Answers

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.

like image 95
Marcelo Avatar answered Sep 03 '25 00:09

Marcelo