jeudi 16 septembre 2021

Constraining a EditText view to a checkBox programmatically in android?

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




Aucun commentaire:

Enregistrer un commentaire