1

I'm inflating views inside a linearlayout dynamically, however once the linear layout reaches the end of the first row, it cuts off the rest and doesn't start on the second row.

enter image description here

for(int a = 0; a < mSkills.get(i).size(); a++){
    View singleSkill = LayoutInflater.from(mContext)
         .inflate(R.layout.singleskill, holder.mSkillLayout, false);
    TextView skillText = singleSkill.findViewById(R.id.singleskilltext);
         skillText.setText(mSkills.get(i).get(a));
         holder.mSkillLayout.addView(skillText);
}

For the linear layout I have it set to wrap_content for the height:

<LinearLayout
        android:id="@+id/ll_skills"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_below="@+id/tv_description"
        android:layout_margin="16dp"/>

I've tried setting it to a defined height e.g 300dp however that doesn't work either. How can I make the layout start on the second row, once the first is full?

4
  • LinearLayout cant do that kind of layouting. It's either only horizontal or vertical. Commented Feb 10, 2020 at 12:45
  • 1
    Linear Layout can either fill views horizontally or vertically so the 2nd row you are expecting cant be done with linear layout only. you can try horizontal scroll view for that to scroll horizontally. Or you can use this 3rd party for flow layout look github.com/nex3z/FlowLayout Commented Feb 10, 2020 at 12:48
  • use github.com/google/flexbox-layout Commented Feb 10, 2020 at 13:00
  • For that you can use GridLayoutManager Commented Feb 10, 2020 at 13:00

3 Answers 3

2

Linear Layout can either fill views horizontally or vertically so the 2nd row you are expecting cant to be done with linear layout only. you can try a horizontal scroll view for that to scroll horizontally. For the exact view-like flow that you described, you can use this 3rd party https://github.com/nex3z/FlowLayout

It can manage the flow of your dynamically inflated view such as if there is no space in the first line then it will put the next view in the second line.

also, you can use material design library chips https://material.io/components/chips/#usage

Sign up to request clarification or add additional context in comments.

1 Comment

Also check out this (google) library: https://github.com/google/flexbox-layout
1

LinearLayout works exactly how it has to be because you specify it as horizontal. For such behavior, you need RecyclerView With GridLayoutManager or create your own layout;).

4 Comments

Is recyclerview necessary because I don't want to recycle anything. I want to show all of them
@davids. of course it depends on your needs. Just pointing you in one of the possible direction. What will you do if your skills list will be more then 100(for example)
good point. But I'm using a swiping library so recyclerviews will have conflict when swiping it.
@davids. actually it won't be a conflict if you disable scroll for RecyclerView.
1

Actually it's doing exactly as it should be, LinearLayout is Linear!, and place its subviews in a single horizontal or vertical row. My advice to you is that create dynamic horizontal LinearLayout as you already doing with TextViews. and put every 3 or 4 textviews (depending on screen size) inside it. and put all LinearLayouts inside one vertical LinearLayout...

Of course in your case, it's not a good idea, the best thing you can do is to use recycler view. but I consider you have problem with that.

2 Comments

Good idea! I think I will use this solution because I'm using a swiping library which will cause an issue when using recyclerview.
Not sure, but try adding nested scroll enabled to false in the recycler view, see if t fixes your problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.