Lucas Kauffman
Lucas Kauffman

Reputation: 6891

Disable landscape on HDPI but not on default landscape

I have an application and the problem is it needs to support tablets and phones. However on tablets you can use landscape instead of portret because it's big enough to display everything. However on a phone this screws everything up, because the screen is too small.

I wanted to disable landscape view for phones and not tablets.

I see that you can define portrait in the manifest.xml file, but is it possible to disable it depending on the layout xml ?

For example:

-phones use layout-hdpi -tablets default to layout

If a device uses layout-hdpi then portrait only should be enabled, else it can go into landscape.

Upvotes: 2

Views: 520

Answers (2)

appmattus
appmattus

Reputation: 2808

An alternative to the answer given by @pjanecze is to do this in code:

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

I call this in my base Activity onCreate method. For some reason the manifest method wasn't working for me.

Some users report problems with the code above being ignored however this can be fixed by first setting the requested orientation to something else first:

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

Upvotes: 0

pjanecze
pjanecze

Reputation: 3175

I did this by following:

  • in manifest as screenOrientation type: "@integer/screenOrientation"
  • for phone (if you want portait) set screenOrientation value to "1"
  • for tablet (if you want landscape) set screenOrientation value to "0"

This values came from source sdk so if you want different check it out (search for screenOrientation in this link).

I hope this solution will help you.

Upvotes: 1

Related Questions