androidkotlinandroid-linearlayout

How to Dynamically Change LinearLayout Orientation Depending on Device Orientation


How do I change the orientation of a LinearLayout when a device changes orientation? What changes would be needed in the *.xml's associated *.kt file?

For example, the device is in Portrait and displays a LinearLayout with a vertical orientation. When the device turns and becomes Landscape, the LinearLayout should have a horizontal orientation.

I imagine that two different layouts would be called depending on the configuration change but I'm not sure.


Solution

  • You need to use separate folders for your layouts : layout for portrait mode and layout-land for landscape mode, and put two separate xml files with the same name in each one.

    One of xml files with LinearLayout with a vertical orientation and another xml file with LinearLayout with horizontal orientation.

    For instance look at How to Add/Create Landscape Layout in Android Studio