Constraining a EditText view to a checkBox programmatically in android?

178 views Asked by At

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

There are 1 answers

4
daniyelp On

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.