Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android ConstraintLayout: How to add a dynamic view one below another

I am trying to add TextViews one below another at runtime in a Constraint Layout. But I always end up with just one textview and the rest hiding behind it. I tried several things including chaining the view, but nothing seems to work.

private void method(int position)
    {
        ConstraintSet set = new ConstraintSet();
        TextView textView = new TextView(getContext());
        int textViewId = 100 + position;
        //previousTextViewId = textViewId;
        textView.setId(textViewId);
        ConstraintLayout.LayoutParams layoutParams = new ConstraintLayout.LayoutParams(0, WRAP_CONTENT);
        layoutParams.rightToRight = PARENT_ID;
        layoutParams.leftToLeft = guideline_60.getId(); //Vertical GuideLine of 60%
        layoutParams.rightMargin = 8;
        textView.setLayoutParams(layoutParams);
        if (Build.VERSION.SDK_INT < 23)
        {
            textView.setTextAppearance(getContext(), R.style.textStyle);
        }
        else
        {
            textView.setTextAppearance(R.style.textStyle);
        }
        textView.setBackgroundColor(backgroundColor);
        textView.setText(categoryName);
        textView.setGravity(Gravity.CENTER);
//markerLayout is the ConstraintLayout 
        markerLayout.addView(textView, position);
        set.clone(markerLayout);
        //set.addToVerticalChain(textView.getId(),previousTextViewId,PARENT_ID);
        set.connect(textView.getId(), ConstraintSet.TOP, markerLayout.getId(), ConstraintSet.TOP, 60);
        set.applyTo(markerLayout);
    }

I am expecting see something like this -

enter image description here

like image 894
sku Avatar asked Mar 03 '17 22:03

sku


2 Answers

You're saying the top of all textviews are connected at the top of the parent:

set.connect(textView.getId(), ConstraintSet.TOP, 
        markerLayout.getId(), ConstraintSet.TOP, 60);

You want to say, the top of one connects to the bottom of the other - something like this:

set.connect(textView.getId(), ConstraintSet.TOP, 
    previousTextView.getId(), ConstraintSet.BOTTOM, 60);

Your method is called method?

like image 147
Nick Cardoso Avatar answered Oct 17 '22 00:10

Nick Cardoso


I know this is old answer but @Temporary saved my day!!. I did same using row layout.I didn't get perfect solution for add row layout dynamically so this might be help to others

Here is a brief example:

activity_main

<androidx.core.widget.NestedScrollView
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:fillViewport="true"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@id/guideline_end"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toEndOf="@id/guideline_start"
        app:layout_constraintTop_toBottomOf="@id/cvFDToolbar">


        <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/clItem"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>


    </androidx.core.widget.NestedScrollView>

Here is row file will add dynamically.

row_main

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


    <TextView
        android:id="@+id/txtTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/_5sdp"
        android:fontFamily="@font/raleway_medium"
        android:text="TextView"
        android:textColor="@color/black"
        android:textSize="@dimen/_14ssp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <EditText
        android:id="@+id/edtValue"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/_5sdp"
        android:background="@color/gray"
        android:inputType="textPersonName"
        android:padding="@dimen/_5sdp"
        android:text="Name"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/txtTitle" />
</androidx.constraintlayout.widget.ConstraintLayout>

Here is code will add row layout dynamically.Add code in your MainActivity file.

val set = ConstraintSet()

        for (i in 0..10) {

            val inflater = LayoutInflater.from(this)

            inflatedLayout = inflater.inflate(
                com.doctor24_7.R.layout.row_pre_consultation_form,
                clItem as ViewGroup,
                false
            )

            inflatedLayout.id = 100 + i
            clItem.addView(inflatedLayout, i)
            set.clone(clItem)
            if (i ==0) {
                set.connect(
                    ConstraintSet.PARENT_ID,
                    ConstraintSet.LEFT,
                    ConstraintSet.PARENT_ID,
                    ConstraintSet.LEFT,
                    60
                )
                set.connect(
                    ConstraintSet.PARENT_ID,
                    ConstraintSet.RIGHT,
                    ConstraintSet.PARENT_ID,
                    ConstraintSet.RIGHT
                )
                set.connect(
                    inflatedLayout.id,
                    ConstraintSet.TOP,
                    ConstraintSet.PARENT_ID,
                    ConstraintSet.TOP
                )
            } else {
                set.connect(
                    ConstraintSet.PARENT_ID,
                    ConstraintSet.LEFT,
                    ConstraintSet.PARENT_ID,
                    ConstraintSet.LEFT
                )
                set.connect(
                    ConstraintSet.PARENT_ID,
                    ConstraintSet.RIGHT,
                    ConstraintSet.PARENT_ID,
                    ConstraintSet.RIGHT
                )
                set.connect(
                    inflatedLayout.id,
                    ConstraintSet.TOP,
                    previousid,
                    ConstraintSet.BOTTOM, resources.getDimension(R.dimen._10sdp).toInt()
                )
            }
            set.applyTo(clItem)
            previousid = inflatedLayout.id

        }
like image 1
Jahanvi Kariya Avatar answered Oct 17 '22 00:10

Jahanvi Kariya