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.

3 Answers 3

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();
}
0
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.

2
  • Thanks for the response. It doesn't seem to work for me.
    – Kannan_SJD
    May 5, 2016 at 9:36
  • can you tell me what is the setContentView() and what is the layout file name in your layout folder? For both mobile and tab May 5, 2016 at 9:52
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.