Andrea Rantin
Andrea Rantin

Reputation: 23

Android Studio constraint set not setting constraints for programmatically added view

I have a layout to which I need to add a view when I focus on a TexInputEditText. I tried creating a textview programmatically, I set an id for it and then I proceeded to clone my constraint layout constraints with a constraintset. The problem is that when I set the constraints for the added textview with constraintset.connect, no matter what I put there the constraints for the textview will always be 0,0 and I can't wrap my head around that. Here's the code:

xml layout:

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

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/headerLayoutParameters"
        android:layout_width="match_parent"
        android:layout_height="69dp"
        android:background="@color/dark"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/parametri"
            android:textColor="@color/white"
            android:textSize="14sp"
            android:textStyle="bold"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

    </androidx.constraintlayout.widget.ConstraintLayout>


    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/constraintParamList"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toBottomOf="@id/headerLayoutParameters"
        >


        <com.google.android.material.textfield.TextInputLayout
            android:id="@+id/textInputLayout23"
            style="@style/customInputText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="16dp"
            android:layout_marginTop="16dp"
            android:layout_marginEnd="16dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">

            <com.google.android.material.textfield.TextInputEditText
                android:id="@+id/paramAnamnesi"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@color/back_light"
                android:drawableLeft="@drawable/parameters_icon"
                android:drawableRight="@drawable/options_icon_points"
                android:drawablePadding="25dp"
                android:inputType="none"
                android:text="@string/anamnesi"
                android:textColor="@color/dark"
                android:textStyle="bold" />

        </com.google.android.material.textfield.TextInputLayout>

        <com.google.android.material.textfield.TextInputLayout
            android:id="@+id/textInputLayout24"
            style="@style/customInputText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="16dp"
            android:layout_marginTop="16dp"
            android:layout_marginEnd="16dp"
            app:layout_constraintTop_toBottomOf="@+id/textInputLayout23"
            tools:layout_editor_absoluteX="16dp">

            <com.google.android.material.textfield.TextInputEditText
                android:id="@+id/paramsPlica"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@color/back_light"
                android:drawableLeft="@drawable/parameters_icon"
                android:drawableRight="@drawable/options_icon_points"
                android:drawablePadding="25dp"
                android:text="@string/plica"
                android:inputType="none"
                android:textColor="@color/dark"
                android:textSize="14sp"
                android:textStyle="bold" />

        </com.google.android.material.textfield.TextInputLayout>

        <com.google.android.material.textfield.TextInputLayout
            android:id="@+id/textInputLayout25"
            style="@style/customInputText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="16dp"
            android:layout_marginTop="16dp"
            android:layout_marginEnd="16dp"
            app:layout_constraintTop_toBottomOf="@+id/textInputLayout24"
            tools:layout_editor_absoluteX="16dp">

            <com.google.android.material.textfield.TextInputEditText
                android:id="@+id/paramsMisureAntro"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@color/back_light"
                android:drawableLeft="@drawable/parameters_icon"
                android:drawableRight="@drawable/options_icon_points"
                android:drawablePadding="25dp"
                android:text="@string/misure_antropometriche"
                android:inputType="none"
                android:textColor="@color/dark"
                android:textSize="14sp"
                android:textStyle="bold" />


        </com.google.android.material.textfield.TextInputLayout>


        <com.google.android.material.textfield.TextInputLayout
            android:id="@+id/textInputLayout26"
            style="@style/customInputText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="16dp"
            android:layout_marginTop="16dp"
            android:layout_marginEnd="16dp"
            app:layout_constraintTop_toBottomOf="@+id/textInputLayout25"
            tools:layout_editor_absoluteX="16dp">

            <com.google.android.material.textfield.TextInputEditText
                android:id="@+id/paramsNote"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@color/back_light"
                android:drawableLeft="@drawable/parameters_icon"
                android:drawableRight="@drawable/options_icon_points"
                android:drawablePadding="25dp"
                android:text="@string/note"
                android:textColor="@color/dark"
                android:inputType="none"
                android:textSize="14sp"
                android:textStyle="bold" />


        </com.google.android.material.textfield.TextInputLayout>

        <com.google.android.material.textfield.TextInputLayout
            android:id="@+id/addParamsLayout"
            style="@style/customInputText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="16dp"
            android:layout_marginTop="16dp"
            android:layout_marginEnd="16dp"
            app:layout_constraintTop_toBottomOf="@+id/textInputLayout26">

            <com.google.android.material.textfield.TextInputEditText
                android:id="@+id/paramsAddParam"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@color/back_light"
                android:drawableLeft="@drawable/add_person"
                android:drawableRight="@drawable/options_icon_points"
                android:drawablePadding="25dp"
                android:text="@string/aggiungi_personalizzato"
                android:textColor="@color/dark"
                android:inputType="none"
                android:textSize="14sp"
                android:textStyle="bold" />


        </com.google.android.material.textfield.TextInputLayout>


    </androidx.constraintlayout.widget.ConstraintLayout>




</androidx.constraintlayout.widget.ConstraintLayout>

Java code:

ConstraintLayout constraintLayout;


 constraintLayout=findViewById(R.id.constraintParamList);



      textInputEditText5.setOnFocusChangeListener(new View.OnFocusChangeListener() { //AGGIUNTA DINAMICA LAYOUT PER NEW PARAM
                @Override
                public void onFocusChange(View v, boolean hasFocus) {
                    if(textInputEditText5.isFocused()){
    
                        EditText editText=new EditText(getApplicationContext());
                        editText.setText("ADDED PARAMETER");
                        ConstraintSet constraintSet=new ConstraintSet();
    
                        int idCreato=8;
                        editText.setId(idCreato);
                        ConstraintLayout.LayoutParams addedParams= new ConstraintLayout.LayoutParams(
                                ConstraintLayout.LayoutParams.WRAP_CONTENT,
                                ConstraintLayout.LayoutParams.WRAP_CONTENT
                        );
                        editText.setLayoutParams(addedParams);
                        constraintLayout.addView(editText);
                        constraintSet.clone(constraintLayout);
                        constraintSet.connect(idCreato, ConstraintSet.TOP,R.id.paramsAddParam, ConstraintSet.BOTTOM, 16);
                        constraintSet.applyTo(constraintLayout);
    
    
    
    
    
                        //TODO FAR FUNZIONARE AGGIUNTA DINAMICA LAYOUT PARAMETRI
    
    
                    }else{
    
                    }
                }
            });

Upvotes: 2

Views: 803

Answers (1)

Cheticamp
Cheticamp

Reputation: 62831

Constraints can only be applied to direct children of ConstraintLayout. You are trying to connect your new view with a TextInputEditText that is a child of a TextInputLayout. Instead of

constraintSet.connect(idCreato, ConstraintSet.TOP,R.id.paramsAddParam, ConstraintSet.BOTTOM, 16);

try

constraintSet.connect(idCreato, ConstraintSet.TOP,R.id.addParamsLayout, ConstraintSet.BOTTOM, 16);

to see if that helps.

Upvotes: 1

Related Questions