Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can we add Views dynamically androidx.constraintlayout.helper.widget.Flow and add reference Ids

How can we add Views dynamically in androidx.constraintlayout.helper.widget.Flow and add reference Ids dynamically.

like image 331
Dev Soni Avatar asked Nov 28 '19 08:11

Dev Soni


People also ask

What is Flow in Constraint layout android?

Due to these types of problems, Google has introduced a feature known as “flows” in the constraint layout 2.0 and above. Flows are used to make the layouts like GridLayouts and LinearLayouts in a much easier and efficient form. Also, it is lightweight for your android phone display.

What is Androidx ConstraintLayout widget ConstraintLayout?

ConstraintLayout. A ConstraintLayout is a ViewGroup which allows you to position and size widgets in a flexible way. ConstraintLayout.LayoutParams. This class contains the different attributes specifying how a view want to be laid out inside a ConstraintLayout .

What is Androidx ConstraintLayout widget guideline?

androidx.constraintlayout.widget.Guideline. Utility class representing a Guideline helper object for ConstraintLayout . Helper objects are not displayed on device (they are marked as View. GONE ) and are only used for layout purposes. They only work within a ConstraintLayout .

What is layer constraint layout?

↳ androidx.constraintlayout.helper.widget.Layer. Layer adds the ability to move and rotate a group of views as if they were contained in a viewGroup Added in 2.0 Methods such as setRotation(float) rotate all views about a common center.


Video Answer


2 Answers

You should add first your view (with an id set) to the parent ConstraintLayout. Then you can add it's reference id to your Flow with Flow.addView(). For example:

val view = LayoutInflater.from(context).inflate(R.layout.item, this, false)
view.layoutParams = ConstraintLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT)
view.id = View.generateViewId()
constraintLayout.addView(view)
flow.addView(view)

with this xml as your ViewGroup:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/constraintLayout"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    >

  <androidx.constraintlayout.helper.widget.Flow
      android:id="@+id/flow"
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:orientation="horizontal"
      app:flow_wrapMode="chain"
      app:layout_constraintBottom_toBottomOf="parent"
      app:layout_constraintEnd_toEndOf="parent"
      app:layout_constraintStart_toStartOf="parent"
      />
</androidx.constraintlayout.widget.ConstraintLayout>
like image 72
Visttux Avatar answered Sep 21 '22 08:09

Visttux


Here is Kotlin Implementation

for (i in 0..4) {
    val customView = CustomComponent (this)
    customView.id = generateViewId()
    constraintLayout.addView(customView,i)
    flow.addView(customView)
}

Following is my XML

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/constraintLayout"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    >

    <androidx.constraintlayout.helper.widget.Flow
        android:id="@+id/flow"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:flow_wrapMode="chain"
        app:flow_horizontalGap="2dp"
        app:flow_verticalGap="2dp"
        app:flow_verticalStyle = "spread_inside"
        app:flow_horizontalStyle="packed"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        />

</androidx.constraintlayout.widget.ConstraintLayout>

Following is my Custom View

class CustomComponent @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyle: Int = 0,
defStyleRes: Int = 0):LinearLayout(context, attrs, defStyle, defStyleRes) {
init {
    LayoutInflater.from(context)
        .inflate(R.layout.custom_view, this, true)
}}
like image 24
angraankit Avatar answered Sep 21 '22 08:09

angraankit