1

I am working with layouts and I need some advice. I have to build this layout

enter image description here

and here is my code

This is my inflated layout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="horizontal"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:background="@drawable/selector_rounded_ingrid"
              android:layout_marginRight="10dp"
              android:layout_marginLeft="10dp">
    <TextView
            android:id="@+id/textview"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:textColor="@color/black"
            android:textSize="25sp"
            android:layout_weight="1"
            android:layout_marginRight="10dp"/>
    <TextView
            android:id="@+id/textviewClose"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="25sp"
            android:text="X"
            android:textColor="@color/black"/>

</LinearLayout>

and this is the linearLayout

View view;
        for (Modifier modifier: list){
            view = View.inflate(mActivity,R.layout.inflated_layout,null);
            ((TextView)view.findViewById(R.id.textview)).setText(modifier.getTitle());
            mDefaultIngridLayout.addView(view);
        }

In result all inflated layout goes to in one line. What should I do here when added item into layout it goes to next line.

pmb
  • 2,327
  • 3
  • 30
  • 47

2 Answers2

1

When dynamically adding childViews to a linearLayout, the views will not be visible when the space is filled. What you need is a view called FlowLayout.

This question will give you lot of information. How can I do something like a FlowLayout in Android?

Also this libraries are very helpful

Community
  • 1
  • 1
SathMK
  • 1,171
  • 1
  • 8
  • 18
0

Use current layout that you have as one line of your layout it means: Wrap your layout with Linear layout with:

          android:orientation="vertical"

and than make another copy of your layout and place this in this vertical wrapper

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical"
          android:layout_width="wrap_content"
          android:background="@drawable/selector_rounded_ingrid"
          android:layout_height="wrap_content"
          android:layout_marginRight="10dp"
          android:layout_marginLeft="10dp">
        <!-- First line-->
 <LinearLayout 
          android:orientation="horizontal"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content">
<TextView
        android:id="@+id/textview"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:textColor="@color/black"
        android:textSize="25sp"
        android:layout_weight="1"
        android:layout_marginRight="10dp"/>
<TextView
        android:id="@+id/textviewClose"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="25sp"
        android:text="X"
        android:textColor="@color/black"/>

 </LinearLayout>
<!-- Second line-->
  <LinearLayout 
          android:orientation="horizontal"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content">
<TextView
        android:id="@+id/textview"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:textColor="@color/black"
        android:textSize="25sp"
        android:layout_weight="1"
        android:layout_marginRight="10dp"/>
<TextView
        android:id="@+id/textviewClose"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="25sp"
        android:text="X"
        android:textColor="@color/black"/>

 </LinearLayout>

</LinearLayout>
Adam Radomski
  • 2,515
  • 2
  • 17
  • 28
  • sorry as I understand I have to set orientation vertical but if I do all view's will be one line – pmb Jan 28 '14 at 07:33