1

I want to update my Constraint layout by adding textfields on button click. so far I'm using constraint set but my layout is not update.

Methond to create additional textfield

private void createUtilForm(){
    titleInputLayout = new TextInputLayout(this);
    int titleId = ViewCompat.generateViewId();
    Log.d("AddEmployee", "createUtilForm: titleId"+ titleId);
    titleInputLayout.setId(titleId);
    ConstraintLayout.LayoutParams clpTitle = new ConstraintLayout.LayoutParams(
            ConstraintLayout.LayoutParams.MATCH_CONSTRAINT, ConstraintLayout.LayoutParams.WRAP_CONTENT);

    childConstraintLayout.addView(titleInputLayout, clpTitle);

    ConstraintSet utilFordSet = new ConstraintSet();

    utilFordSet.clone(childConstraintLayout);
    utilFordSet.connect(titleInputLayout.getId(), ConstraintSet.START, ConstraintSet.PARENT_ID, ConstraintSet.START);
    utilFordSet.connect(titleInputLayout.getId(), ConstraintSet.END, ConstraintSet.PARENT_ID, ConstraintSet.END);
    utilFordSet.connect(titleInputLayout.getId(), ConstraintSet.TOP, allowanceHeaderTV.getId(), ConstraintSet.BOTTOM);
    utilFordSet.connect(deductionHeaderTV.getId(), ConstraintSet.TOP, titleInputLayout.getId(), ConstraintSet.BOTTOM);

    utilFordSet.applyTo(childConstraintLayout);
}

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:id="@+id/main_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".AddEmployeeActivity">
    <ScrollView
        android:id="@+id/child_scrollview"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/add_employee_toolbar">

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


            <TextView
                android:id="@+id/allowance_header_tv"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginStart="16dp"
                android:layout_marginTop="16dp"
                android:layout_marginEnd="8dp"
                android:text="@string/allowance_header_hint"
                android:textSize="24sp"
                app:layout_constraintEnd_toStartOf="@+id/allowance_add_ib"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="parent" />

            <ImageView
                android:id="@+id/allowance_add_ib"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginEnd="16dp"
                app:layout_constraintBottom_toBottomOf="@+id/allowance_header_tv"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintTop_toTopOf="@+id/allowance_header_tv"
                app:srcCompat="@drawable/ic_add" />

            <TextView
                android:id="@+id/deduction_header_tv"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginStart="16dp"
                android:layout_marginTop="16dp"
                android:layout_marginEnd="8dp"
                android:text="@string/deduction_header_hint"
                android:textSize="24sp"
                app:layout_constraintEnd_toStartOf="@+id/deduction_add_ib"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/allowance_header_tv" />

            <ImageView
                android:id="@+id/deduction_add_ib"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginEnd="16dp"
                android:contentDescription="@string/img_desc_label"
                app:layout_constraintBottom_toBottomOf="@+id/deduction_header_tv"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintTop_toTopOf="@+id/deduction_header_tv"
                app:srcCompat="@drawable/ic_add" />

        </androidx.constraintlayout.widget.ConstraintLayout>
    </ScrollView>

</androidx.constraintlayout.widget.ConstraintLayout>
Cœur
  • 37,241
  • 25
  • 195
  • 267
Ismail
  • 445
  • 1
  • 5
  • 20
  • Is it obvious that you tried to make a [good question](https://stackoverflow.com/help/how-to-ask) doing your best. The only problem I see here is that your English is a little hard to understand. I will try to guess what you try to say the best I can as well. Perhaps you are trying to bind your method to a button, so when you click it the method executes? – SebasSBM Sep 15 '18 at 16:07
  • It looks like your ` – SebasSBM Sep 15 '18 at 16:11
  • However, your question seems to be a duplicate of [Android: how to handle button click](https://stackoverflow.com/q/14782901/3692177). – SebasSBM Sep 15 '18 at 16:40
  • Possible duplicate of [Android: how to handle button click](https://stackoverflow.com/questions/14782901/android-how-to-handle-button-click) – SebasSBM Sep 15 '18 at 16:42
  • Hi, @SebasSBM, I just realized, I should have structured the question properly. I wanted to create Views programmatically(using constraint layout) so that I can set their ids. – Ismail Sep 26 '18 at 11:58
  • @Ismali You can edit your question to re-structure it if you think it might help. – SebasSBM Sep 26 '18 at 12:24

2 Answers2

0

Here is what I did in past..!

1: Just create an empty linear layout inside the constraint layout of your Acitivity.XML file and define constraints if any.

2: Create a textfield.XML file for your text fields

3: Add this code in your Activity java.

 public void addLayout() {


    LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);

    final View row = inflater.inflate(R.layout.textfield, null);

    linearLayout.addView(row, linearLayout.getChildCount() - 1);

}

but If you have a fragment java change into this

 LayoutInflater inflater = (LayoutInflater)getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

And how to Remove.

  public void RemoveLayout() {
        if (linearLayout.getChildCount() > 0) {
            linearLayout.removeViewAt(linearLayout.getChildCount() - 1);
        }
    }
Rohaitas Tanoli
  • 624
  • 4
  • 16
0

Supposing your button is like this in your XML layout:

<Button
    android:id="@+id/addTextViewButton"
    [...] />

You can get a reference to the button like this:

Button myButton = findViewById(R.id.addTextViewButton);

If you make the button from java code, you will need it stored in some variable to attach the method.

You can attach a method to your button like this. Add this within your Activity's onCreate method:

myButton.setOnClickListener = new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        createUtilForm();
    }
});

But this code could be structured in other ways. In this link other ways to implement OnClickListeners are explained.

SebasSBM
  • 860
  • 2
  • 8
  • 32