3

I want to add dynamic view into a Horizontal LinearLayout. But my issue is if there is no space at Horizontal it should automatically placed on vertically.

My Image

enter image description here

My Expected Result

enter image description here

My Codes are

    for (int j = 0; j < jobDet.getKeywords().length; j++) {

               RelativeLayout tr_head = (RelativeLayout) getLayoutInflater().inflate(R.layout.tag_lay, null);

               TextView label_date = (TextView) tr_head.findViewById(R.id.tag_name);
               label_date.setText(jobDet.getKeywords()[j]);

               keywordL.addView(tr_head, new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT));

           }

My "tag_lay.xml"

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout  xmlns:android="http://schemas.android.com/apk/res/android"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content">
         <TextView
             android:layout_width="wrap_content"
             android:layout_height="30dp"
             android:id="@+id/tag_name"
             android:paddingLeft="10dp"
             android:paddingRight="10dp"
             android:background="@drawable/round_edit_box"
             android:text="     PHP     "
             android:gravity="center"
             android:lines="1"
             android:layout_marginRight="5dp"/>

      </RelativeLayout>
Onkar Nene
  • 1,359
  • 1
  • 17
  • 23
Mahbub Mukul
  • 333
  • 3
  • 18

1 Answers1

9

You can not do that with single LinearLayout because it has either VERTICAL or HORIZONTAL orientation. I would suggest you to take a look at google's FlexboxLayout. Its a library which provides a layout where you can achieve similar view. You can add this library by adding below line to your app level gradle file:

compile 'com.google.android:flexbox:0.1.3'

You can change the keywordL to FlexboxLayout from LinearLayout. A sample code for container layout can be something like below:

<com.google.android.flexbox.FlexboxLayout xmlns:android="http://schemas.android.com/apk/res/android"
         xmlns:app="http://schemas.android.com/apk/res-auto"
         android:id="@+id/flexbox_layout"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         app:alignContent="flex_start"
         app:alignItems="flex_start"
         app:flexWrap="wrap"/>

You can change the value of alignContent, alignItems and flexWrap according to your requirement. Although this should work because it is working for me. While adding the childs you can do something like below:

for (int j = 0; j < jobDet.getKeywords().length; j++) {

           RelativeLayout tr_head = (RelativeLayout) getLayoutInflater().inflate(R.layout.tag_lay, keywordL, false );

           TextView label_date = (TextView) tr_head.findViewById(R.id.tag_name);
           label_date.setText(jobDet.getKeywords()[j]);
           keywordL.addView(tr_head);
       }

Please let me know if you have trouble implementing.

himanshu1496
  • 1,921
  • 19
  • 34