Reputation: 4082
I am new to android. I am trying to create a list from a JSON array from server.
I would likes to show 2 item in a row like the following image, and the third one will fall under the 1 and fourth one under two. Just like "float:left" in CSS.
I am using LinearLayout inside a ScrollView. I am confused how to arrange the dynamically created LinearLayout like the image ?
Thanks in advance
Upvotes: 0
Views: 290
Reputation: 151
Create an outer LinearLayout with orientation set to vertical.
Create the rest of the LinearLayouts(for each row) in the loop. If index%2 (2 can be replaced with any other constant that determines the number of vies on the row) is 0, create a new layout and add the items to it, else create a new layout. The inner layouts created in the loop should have orientation set to horizontal. Also, keep adding each inner layout after they are created.
You can make use of GridView/ListView straight away so as to reduce the complexity of the code and if there is no specific reason to use a Linear Layout. There are lots of tutorials that will help you in achieving this.
Upvotes: 0
Reputation: 3007
Look into using a GridView for this. You define how many items show in one row amongst other things. It aligns as a Grid, just like the image(s) you have posted.
http://developer.android.com/guide/topics/ui/layout/gridview.html
From there you can use a custom GridView to inflate a custom layout so it displays it shows up just how you want. There are many tutorials out there explaining this. Good luck.
Upvotes: 1