michael
michael

Reputation: 110530

Android does not pick up layout file in res/layout-land

In My Manifest.xml file, if I have the following 'android:configChanges' for MyActivity, it does not pick up my layout file in res/layout-land when I rotate my phone to landscape mode.

    <activity
        android:name="MyActivity"
        android:configChanges="orientation|keyboard|keyboardHidden">
    </activity>

Can you please tell me how can I fix it?

Thank you.

Upvotes: 1

Views: 4776

Answers (3)

javahead76
javahead76

Reputation: 1050

I understand wanting to iterate best practices, but these answers don't necessarily solve the problem. I also agree with these best practices. Unfortunately, they cannot in all cases be practiced.

I would consider what the OP stated as the issue to be a bug in Android. saying I will handle "configChanges" in this Activity to Android means, if the configuration changes, don't kill my Activity. The use of the word "changes" does not imply that the initial configuration passed to the Activity will be ignored by the Activity. Either the verbiage should be changed, or the Activity should handle the proper orientation in the original configuration.

The workaround is to manually select the proper layout file by checking the orientation manually. Essentially, don't use -land buckets in configuration with configChanges="orientation|keyboard|keyboardHidden", the land buckets will be ignored.

// done in onCreate
int desiredLayoutId;
int orientation = getResources().getConfiguration().orientation;
if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
    desiredLayoutId = R.layout.some_layout_landscape;
} else {
    desiredLayoutId = R.layout.some_layout_portrait;
}
setContentView(desiredLayoutId);

Again, I don't like this, but if you are at the "last resort" and decide to manage your own orientation, you can follow the code I provided to get the proper layout file. One possible use case might be when creating an activity with a streaming video that should not unload and reload when the screen rotates.

Upvotes: 2

Phil Lello
Phil Lello

Reputation: 8639

By including orientation in configChanges, you're disabling the default layout-change behaviour, which is to re-start your activity in the new orientation.

You'll need to manually change the layout used when you get CONFIGURATION_LANDSCAPE/CONFIGURATION_PORTRAIT in onConfigurationChanged.

Note that according to the Activity docs, onConfigurationChanged is a 'last resort' option - it's generally better to persist state and let the system re-launch your app.

Best wishes,

Phil Lello

Upvotes: 3

ferostar
ferostar

Reputation: 7082

Without knowing the rest of the Manifest or your activity, all i can said is remove the "orientation" from android:configChanges. That disables the orientation change.

Upvotes: 4

Related Questions