2

I am developing an app that needs to run strictly in portrait mode in smart phones and strictly in landscape mode in tablets. I am using the following code to set the orientation manually in onCreate method of my login activity:

 if (getResources().getBoolean(R.bool.portrait_only)) {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        } else {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
        }

Where the bool value comes false in Tablets. However the orientation is changed to landscape; still the layout file is picked from "layout_large" folder instead of "layout_large_land" folder. Hence my view looks stretched.

I have tried this link.

But it doesn't help.!

Any help is appreciated.

Thanks in Advance.

Community
  • 1
  • 1
Kannan_SJD
  • 1,022
  • 2
  • 13
  • 32

3 Answers3

2

I had the same problem. Make sure you're NOT setting android:configChanges="orientation" for your activity in AndroidManifest.

I'm guessing the problem with that is that setRequestedOrientation potentially triggers an orientation config change, HOWEVER by using android:configChanges="orientation" this config change is not registered by the system.

Alternatively, if you want to keep the Manifest setting, you would have to implement your own recreation mechanic for this particular configuration change.

It may look something like this:

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    if(getResources().getBoolean(R.bool.portrait_only))
        recreate();
}
Bjelis
  • 116
  • 7
1

I have used a folder like this for Tab & mobile

enter image description here

layout folder is for mobile and layout-sw720dp for Tabs. 720 dp is width which tells 720+ dp will use that folder for layout.

Community
  • 1
  • 1
Jimit Patel
  • 4,265
  • 2
  • 34
  • 58
0

Being late in this thread but will help in the future SO. In my case I was programmatically changing the orientation of the screen by calling

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

OR

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

depending on the user's action. I have also created a separate layout for portrait and landscape but every time on changing the screen from portrait to landscape, it always loading the portrait screen in landscape.

In AndroidManifest.xml, I've set this

 android:configChanges="screenSize|smallestScreenSize|screenLayout|orientation"

So, To solve this problem, Need to remove the orientation from android:configChanges then it works. This is what android documentation Link mentioned

The "orientation" value prevents restarts when the screen orientation changes.

Suraj Bahadur
  • 3,730
  • 3
  • 27
  • 55