0

I have to develop an Activity to show a list of items. In portrait I have to display 3 items in a single row in landscape 4 items.

What should be the best way to implement this solution?

Alex K
  • 8,269
  • 9
  • 39
  • 57
  • get orientation of device with http://stackoverflow.com/questions/2795833/check-orientation-on-android-phone, then calculate height of screen and pass height / 4 or height / 3 to adapter constructor and in `getView` set this height to your view – Shayan Pourvatan Dec 29 '14 at 08:46
  • @shayan pourvatan do you think that I can use two different versions of adapter? one with 3 elements and one with 4? – user2672950 Dec 29 '14 at 08:50
  • you can have two adapter but for setting number element you must calculate height of row in code, you can't set that via xml – Shayan Pourvatan Dec 29 '14 at 08:53
  • You can add your required items in each xml.. Check my answer below. – Subhalaxmi Dec 29 '14 at 12:22

3 Answers3

2

You have to create one more layout like res/layout-land and have to create same xml in that folder for respective design in landscape mode. You can put views as per your requirement in each xml file. So on orientation change as per requirement it will pick your respective design from respective folder.

Check The image for more detailsenter image description here

Note: here frgtpw.xml is present in both Layout Folder .

For more put your doubts here :)

Subhalaxmi
  • 5,687
  • 3
  • 26
  • 42
0

Provide tow layouts with a same name but one with 3 elements and the other with 4 elements. Put one of them in the layout-port and another in layout-land. Then in your adapter find 4 elements and set the values for those but for the fourth item just check if it is null or not, if it is null that means it is portrait else it means landscape.

mmlooloo
  • 18,937
  • 5
  • 45
  • 64
  • how you want set " 3 elements " in layout? – Shayan Pourvatan Dec 29 '14 at 09:09
  • @shayanpourvatan 3 elements means for example 3 imageviews or 3 textviews that is defined already, the op dose not ask it to dynamically add elements, so the layout of portrait and landscape are already known. – mmlooloo Dec 29 '14 at 09:31
0

in your adapter implementation you can define as

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    if (convertView == null) {
       if(isPortrait){
          convertView = LayoutInflater.from(getContext()).inflate(R.layout.portrait_layout, parent, false);
       }else{
          convertView = LayoutInflater.from(getContext()).inflate(R.layout.lanscape_layout, parent, false);
       }

     }


   }
VikasGoyal
  • 3,308
  • 1
  • 22
  • 42
  • This solution could work, but I have a question: how can I set the items in the adapter? I mean if I have a list of objects and I have to populate the adapter, in position 0 I have to put elements from List in position 0, but the position 1 should be the 1st element of the second row right? – user2672950 Dec 29 '14 at 10:02
  • please clarify your question about position updation – VikasGoyal Dec 29 '14 at 10:22