0

At the click of a button, I create one textview programmatically and add them to flexboxlayout. Why is only one view added?

mButton.setOnClickListener(new View.OnClickListener() {
        int i = 1;
        @Override
        public void onClick(View v) {
            flexboxLayout.setFlexDirection(FlexDirection.ROW_REVERSE);
            flexboxLayout.setFlexWrap(FlexWrap.WRAP);
            flexboxLayout.setJustifyContent(JustifyContent.CENTER);
            final TextViewR textViewR = new TextViewR(getApplicationContext(), "Text " + Integer.toString(i++)); //setting text here
            FlexboxLayout.LayoutParams lp = new FlexboxLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            lp.setOrder(1);
            textViewR.setLayoutParams(lp);
            flexboxLayout.addView(textViewR);
        }
    });
ravi
  • 899
  • 8
  • 31

1 Answers1

0

I assume TextViewR is your custom TextView.

You should change

`FlexboxLayout.LayoutParams lp = new FlexboxLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);`

to

`FlexboxLayout.LayoutParams lp = new FlexboxLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);` 

since textViewR.setLayoutParams(lp); is setting the flexboxlayout parameters to the textview and your parameters are telling the textview to match parent which is the flexbox.

Sachin Rajput
  • 4,326
  • 2
  • 18
  • 29
ravi
  • 899
  • 8
  • 31