Rameez Hussain
Rameez Hussain

Reputation: 6754

Change layout without calling onCreate

I have an activity with two layout files, one each for portrait and landscape modes. I see that the onCreate method is called each time I change orientation. I tried using

android:configChanges = "orientation"

in the manifest file, but it only reads the portrait layout. Is there a way I can set the layout to the landscape mode without calling onCreate?

Upvotes: 3

Views: 1235

Answers (4)

Squonk
Squonk

Reputation: 48871

If you use android:configChanges = "orientation", you must override the onConfigurationChanged(...) method of your Activity. Check the newConfig parameter for the new orientation then manually set the layout you want.

See the following links:

onConfigurationChanged(Configuration newConfig)

Handling the Configuration Change Yourself

Upvotes: 2

ViliusK
ViliusK

Reputation: 11555

If you want your activity be only in landscape mode, you set this attribute for the activity in the AndroidManifest.xml:

        android:screenOrientation="landscape"

You can put your layout.xml files in such folders for different screen orientations:

  • res\layout-land
  • res\layout-port

Upvotes: 0

barconr
barconr

Reputation: 197

This is a fact of life with android. You need to develop your activity around the fact it can be destroyed at any minute due to a configuration change like the screen's orientation changing. Save all the the information used to build the screen in on pause and recreate it in onCreate.

Upvotes: 0

Ahmad
Ahmad

Reputation: 72553

You could create two layout folders. One for portrait mode and one for landscape. layout and layout-land.

Upvotes: 0

Related Questions