0

What I am trying here is, when the addNewToDo button is clicked, the defaultEditText view should be converted from editText view into a checkBox view with the same constraints as the defaultEditText, and I am quite successful in doing so Using the Constraint.LayoutParams.

But what I also want is that at the same time when the button is clicked, a newEditText should be created with the same properties as the previous one(editText) and place at the bottom of the checkBox that is created programmatically and also the addNewToDo button should go below the newEditText

Below code succeeds at,

creating the checkBox with the intended properties.
creating a new editText view with the right width and height.
constraining the button below the newly created editText.

fails at,

Constraining the newEditText view below the checkBox that's been created

The output I am getting is that,

The newEditText is getting placed at the top left corner of the UI, I learned that the views get placed there if it isn't constraint properly

enter image description here

    Button addNewToDo = findViewById(R.id.addNewToDo);
    addNewToDo.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

                CheckBox toDoCheckBox = new CheckBox(MainActivity.this);
                toDoCheckBox.setText(defaultEditText.getText().toString());
                toDoCheckBox.setTextSize(20);
                toDoCheckBox.setId(View.generateViewId());

                ConstraintLayout.LayoutParams params = (ConstraintLayout.LayoutParams) defaultEditText.getLayoutParams();

                ConstraintLayout.LayoutParams newParams = new ConstraintLayout.LayoutParams(
                        ConstraintLayout.LayoutParams.MATCH_PARENT,
                        ConstraintLayout.LayoutParams.MATCH_PARENT
                );

                newParams.width = params.width;
                newParams.height = params.height;
                newParams.startToStart = params.startToStart;
                newParams.leftToLeft = params.leftToLeft;
                newParams.topToTop = params.topToTop;
                newParams.leftMargin = params.leftMargin;
                newParams.topMargin = params.topMargin;

                constraintLayout.addView(toDoCheckBox,-1,newParams);
                defaultEditText.setVisibility(View.INVISIBLE);

                //creating a new editText
                EditText newEditText = new EditText(MainActivity.this);
                newEditText.setWidth(defaultEditText.getWidth());
                newEditText.setHeight(defaultEditText.getHeight());
                newEditText.setId(View.generateViewId());

                constraintSet.clone(constraintLayout);
                constraintSet.connect(newEditText.getId(),ConstraintSet.START,toDoCheckBox.getId(),ConstraintSet.START);
                constraintSet.connect(newEditText.getId(),ConstraintSet.END,toDoCheckBox.getId(),ConstraintSet.END);
                constraintSet.connect(newEditText.getId(),ConstraintSet.TOP,toDoCheckBox.getId(),ConstraintSet.BOTTOM);
                constraintSet.connect(addNewToDo.getId(),ConstraintSet.TOP,newEditText.getId(),ConstraintSet.BOTTOM);

                constraintSet.applyTo(constraintLayout);
                constraintLayout.addView(newEditText);


        }

    });

How to constraint the newEditText to the checkBox?

FYI, I am the using ContraintLayout within the ScrollView

1 Answers1

0

UPDATE

What you did in your code doesn't follow the rules Cheticamp stated: Setting constraints programmatically via constraintSet causes views to disappear.

Taking those into consideration, you should move constraintLayout.addView(newEditText); right after newEditText.setId(View.generateViewId());

I think this is all what's wrong to your code.

OLD ANSWER (also solves the issue)

Try this:

//creating a new editText

EditText newEditText = new EditText(MainActivity.this);
newEditText.setWidth(defaultEditText.getWidth());
newEditText.setHeight(defaultEditText.getHeight());
newEditText.setId(View.generateViewId());

ConstraintLayout.LayoutParams lp =
        new ConstraintLayout.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT
        );

lp.topToBottom = toDoCheckBox.getId();

newEditText.setLayoutParams(lp);

constraintLayout.addView(newEditText);

It should work. If it does, you can add the rest of the constraints:

lp.startToStart = toDoCheckBox.getId();

I'm waiting for some feedback.

daniyelp
  • 915
  • 1
  • 11
  • 26
  • Your solution worked perfectly, but I still don't understand why the costraintSet I created didn't constraint the editText to the checkBox dynamically as it also constraint startToStart, endToEnd and TopToBottom as well, for reference look the question I asked there is the constraintSet i created. – mockingbird Sep 17 '21 at 06:34
  • @mockingbird I solved it this way because this is what I've been using. Didn't know there is another way to set the constraints up until you answer. I've tried to do it your way and found an issue in your logic. See the updated answer. – daniyelp Sep 17 '21 at 07:21
  • @mockingbird How is it going? – daniyelp Sep 26 '21 at 17:08
  • 1
    Yeah going fine man, i haven't checked whether your solution works or not, i'll give it a try and ill give you the feedback man. – mockingbird Sep 27 '21 at 05:52