0

I am struggling at one thing. Let's say I have linear layout with set height and width is match_parent. I will have a set number of views 1 to 6 and I don't know at runtime how much I will receive from server. The problem is, how can I sort them in a layout so they scale their width accordingly to number of views present ? If there are more than 3 views I need to put them in two lines.this is how it should look like

I was thinking about using layout weight, but can't think about solution that can put them to two lines. Any ideas ?

Lukas Anda
  • 716
  • 1
  • 9
  • 30

2 Answers2

0

You should use listview in your rooted LinearLayout and design single list item in an other xml layout , create a custom adapter for your single list item and set the adapter to your list view . this answer will help you how to create custome adapter

Community
  • 1
  • 1
Zar Saeed
  • 100
  • 2
  • 13
0

You have to dynamically add views to linear layout.

First create container layout in xml.

<LinearLayout
     android:id="@+id/containerLayout"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
/>

Then check,

 if(list.size()<=3)
    {  
         Then assign weight to container. i.e weight=list.size

          for(int i=0;i<size;i++)
          {
            TextView textview = new TextView(this);
            textview.setText(brandName);
            textview.setWeight(1f);

            container.addView(textview);
          }
    }
    else
   {

         int totalRows= (list.size/3)+(list.size%3);
         int count=0;

        for(int i=0;i<totalRows;i++)
        {
               LinearLayout newLL = new LinearLayout(mContext);
                newLL.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
                newLL.setOrientation(LinearLayout.HORIZONTAL);

                for(int j=count;j<count+3;j++)
                {
                    count++;
                    TextView textview = new TextView(this);
                    textview.setText(brandName);
                    newLL.addView(textview);
                }
                container.addView(newLL);
        }

   }

You have to do something like this.This is not actual code.

Ragini
  • 332
  • 1
  • 4
  • 16