androidandroid-flexboxlayout

set FlexboxLayout's attribute layout_wrapbefore programmatically


I have flexbox-layout contains TextView that have the attribute:

app:layout_wrapBefore="true" 

but I need to set it to false programmatically!

any idea on how to do that?


the xml, if needed!

<com.google.android.flexbox.FlexboxLayout
        android:id="@+id/flexboxlayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        app:alignContent="stretch"
        app:alignItems="stretch"
        app:flexWrap="wrap"
        app:justifyContent="flex_end">


        <TextView
            android:id="@+id/mainText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="5dp"
            android:text="07:22 PM"
            android:textSize="12sp"
            app:layout_alignSelf="center"
            app:layout_wrapBefore="true" />

    </com.google.android.flexbox.FlexboxLayout>

Solution

  • You must use setWrapBefore method as described in its LayoutParams and set those layout params to the TextVuew again.

    TextView mainTextView = findViewById(R.id.mainText); // or get it as you should
    FlexboxLayout.LayoutParams lp = (FlexboxLayout.LayoutParams) mainTextView.getLayoutParams();
    lp.setWrapBefore(false);
    mainTextView.setLayoutParams(lp);