0

How can I get one column in portrait mode and two columns in landscape mode with a ListView or TableLayout.

For example, in Portrait:

  • Element 1
  • Element 2
  • Element 3
  • Element 4

And in Landscape :

  • Element1 Element2
  • Element3 Element4

I thought to use ListView with one column in portrait mode and TableLayout with two columns in landscape mode, but before that, I want to know your suggestions.

Blumer
  • 5,005
  • 2
  • 33
  • 47
OWZY
  • 531
  • 7
  • 15

2 Answers2

1

I recommend using a ListView rather than a TableLayout, since ListViews only create enough Views to fill the screen while a TableLayout will create every resource whether they are visible or not. In this way a TableLayout is slower and might even crash your app.


You can check the device's orientation with one of the answers from: Check orientation on Android phone. Then if the orientation is:

  • landscape, load your double View layout.
  • portrait, load your basic, single View layout.

Loading different layouts in a ListView requires a simple custom Adapter. If you haven't written one before, here is a tutorial to help you.

Community
  • 1
  • 1
Sam
  • 86,580
  • 20
  • 181
  • 179
  • A GridView is a subclass of ListView, so they are quite similar. But to create a fast, personalized layout you'll have to write a customized Adapter anyway. So if one if-statement is "way difficult" then definitely use a GridView. :) But you'll still need use two layouts or use an if-statement to determine the orientation. – Sam Feb 08 '13 at 23:48
1

It'd be way easy for you to use a GridView. You can set/choose number of columns on runtime and hence set them differently in portait vs landscape mode. That way, you'd just be manipulating a single view.

Gaurav Arora
  • 1,805
  • 13
  • 13