Ankur Khandelwal
Ankur Khandelwal

Reputation: 1070

How to programmatically add view in Constraint layout?

I am trying to design the below layout

<android.support.constraint.ConstraintLayout
            android:id="@+id/before_breakfast_option"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <TextView
                android:id="@+id/diabetes_text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="16dp"
                android:layout_marginStart="16dp"
                android:text="water"
                android:textAppearance="@style/TextAppearance.AppCompat.Medium"
                android:textColor="@color/black"
                app:layout_constraintBaseline_toBaselineOf="@+id/toogle_diabeties"
                app:layout_constraintLeft_toLeftOf="parent"/>

            <TextView
                android:textColor="@color/black"
                android:text="almonds"
                app:layout_constraintTop_toTopOf="parent"
                android:id="@+id/toogle_diabeties"
                app:layout_constraintRight_toRightOf="parent"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>

        </android.support.constraint.ConstraintLayout>

using the below code:

 var textView= TextView(this@DietStepFive)
                    textView.id=100
                    textView.text="water"
                    textView.background=ContextCompat.getDrawable(this@DietStepFive,R.drawable.rectangle_diet)
                    textView.setTextColor(ContextCompat.getColor(this@DietStepFive,R.color.black))

                    var textView1= TextView(this@DietStepFive)
                    textView1.id=101
                    textView1.text="almonds"
                    textView1.background=ContextCompat.getDrawable(this@DietStepFive,R.drawable.rectangle_diet)
                    textView1.setTextColor(ContextCompat.getColor(this@DietStepFive,R.color.black))

                    var constraintset= ConstraintSet()
                    constraintset.clone(before_breakfast_option)

                    //left to left of
                    constraintset.connect(textView.id,ConstraintSet.LEFT,ConstraintSet.PARENT_ID,ConstraintSet.LEFT,0)
                    //baseline
                    constraintset.connect(textView.id,ConstraintSet.BASELINE,textView1.id,ConstraintSet.BASELINE,0)
                    //right to right of
                    constraintset.connect(textView1.id,ConstraintSet.RIGHT,ConstraintSet.PARENT_ID,ConstraintSet.RIGHT,0)
                    //top to top of
                    constraintset.connect(textView1.id,ConstraintSet.TOP,ConstraintSet.PARENT_ID,ConstraintSet.TOP,0)

                    constraintset.applyTo(before_breakfast_option)

                    before_breakfast_option.addView(textView)
                    before_breakfast_option.addView(textView1)

But the XML code is giving me the layout which has two textview one is one left side and one is one right side but kotlin code is giving me the both the textview overlapping on left side. Why?

what wents wrong? any lead?

Upvotes: 1

Views: 5014

Answers (3)

Ankur Khandelwal
Ankur Khandelwal

Reputation: 1070

Maybe anyone else will use it in future. A sound sleep and work done. I was using the wrong constraint.

Instead of this

//left to left of
constraintset.connect(textView.id,ConstraintSet.LEFT,ConstraintSet.PARENT_ID,ConstraintSet.LEFT,0)
//baseline
constraintset.connect(textView.id,ConstraintSet.BASELINE,textView1.id,ConstraintSet.BASELINE,0)
//right to right of
constraintset.connect(textView1.id,ConstraintSet.RIGHT,ConstraintSet.PARENT_ID,ConstraintSet.RIGHT,0)
//top to top of
constraintset.connect(textView1.id,ConstraintSet.TOP,ConstraintSet.PARENT_ID,ConstraintSet.TOP,0)

use this

//left to right of
constraintset.connect(textView1.id,ConstraintSet.LEFT,textView.id,ConstraintSet.RIGHT,10)
//baseline
constraintset.connect(textView1.id,ConstraintSet.BASELINE,textView.id,ConstraintSet.BASELINE,0)

Upvotes: 0

Cheticamp
Cheticamp

Reputation: 62841

Add the TextViews to the layout then connect them just like you did when setting up the XML. You added the views THEN connected them.

Move

before_breakfast_option.addView(textView)
before_breakfast_option.addView(textView1)

before

var constraintset= ConstraintSet()

and everything should work.

Upvotes: 3

Y.Benziane
Y.Benziane

Reputation: 1

Try to replace app:layout_constraintRight_toRightOf="parent"

with app:layout_constraintRight_toRightOf="@+id/toogle_diabeties"

Upvotes: -2

Related Questions