Search code examples
androidandroid-layoutlistviewandroid-fragments

Different list view layouts for different screen orientations


I have a view on which I would like to place two ListViews that would have position next to each other depending on the screen orientation. It would look like that:

Horizontal screen orientation:

 ___________ ___________
|           |           |
| ListView1 | ListView2 |
|___________|___________|

Vertical screen orientation:

 ___________ 
|           |
| ListView1 |
|___________|
|           |
| ListView2 |
|___________|

From what I have read, I need to use a fragment for each ListView - can it be done in some other way (i.e. just by using two different XML layout files but the same logic)?


Solution

  • You can do with following approach.

    Just create a directory named layout-land and layout-port. Put layout file with same name. Make sure you are using same ids inside layouts.

    layout-land
        - myactivity.xml
    layout-port
        - myactivity.xml
    

    Android will automatically take layout as per orientation.

    Reference : Multiple Screen Support. Read Using configuration qualifiers in it..