1

My question follows on from this question: Android : How to programatically set layout_constraintRight_toRightOf "parent".

I have the written the following code that should constrain the new TextView to either the left (btnSendL) or the right (btnSendR) of a ConstraintLayout.

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    final ConstraintLayout clMessagesContainer = (ConstraintLayout) findViewById(R.id.clMessagesContainer);
    final EditText txtMessage = (EditText) findViewById(R.id.txtMessage);
    Button btnSendL = (Button) findViewById(R.id.btnSendL);
    Button btnSendR = (Button) findViewById(R.id.btnSendR);

    btnSendL.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v) {

            TextView lblNxtMessage = new TextView(MainActivity.this);
            lblNxtMessage.setLayoutParams(new ConstraintLayout.LayoutParams(
                    ConstraintLayout.LayoutParams.WRAP_CONTENT,
                    ConstraintLayout.LayoutParams.WRAP_CONTENT));

            lblNxtMessage.setText(txtMessage.getText().toString());
            clMessagesContainer.addView(lblNxtMessage);

            ConstraintSet csL = new ConstraintSet();
            csL.clone(clMessagesContainer);
            csL.connect(lblNxtMessage.getId(), ConstraintSet.LEFT, ConstraintSet.PARENT_ID, ConstraintSet.LEFT, 0);
            csL.connect(lblNxtMessage.getId(), ConstraintSet.TOP, ConstraintSet.PARENT_ID, ConstraintSet.TOP, 0);
            csL.applyTo(clMessagesContainer);

        }
    });

    btnSendR.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v) {

            TextView lblNxtMessage = new TextView(MainActivity.this);
            lblNxtMessage.setLayoutParams(new ConstraintLayout.LayoutParams(
                    ConstraintLayout.LayoutParams.WRAP_CONTENT,
                    ConstraintLayout.LayoutParams.WRAP_CONTENT));

            lblNxtMessage.setText(txtMessage.getText().toString());
            clMessagesContainer.addView(lblNxtMessage);

            ConstraintSet csR = new ConstraintSet();
            csR.clone(clMessagesContainer);
            csR.connect(lblNxtMessage.getId(), ConstraintSet.RIGHT, ConstraintSet.PARENT_ID, ConstraintSet.RIGHT, 0);
            csR.connect(lblNxtMessage.getId(), ConstraintSet.TOP, ConstraintSet.PARENT_ID, ConstraintSet.TOP, 0);
            csR.applyTo(clMessagesContainer);
        }
    });


}

However, each time I click either, this is done so for all TextViews already created, instead of only the TextView created inside the most recent onClickListenerMethod. How can I fix this?

2 Answers2

0

This happens because each newly created View has a default ID of -1, so constraints applied to any of them affect all Views with that ID.

To fix this, you need to generate a unique ID for every created View. The simplest way is to use a static method generateViewId() from the View class. The usage would look like this:

TextView lblNxtMessage = new TextView(MainActivity.this);
lblNxtMessage.setId(View.generateViewId());
Pawel Laskowski
  • 6,078
  • 1
  • 21
  • 35
0

I recommend setId() to new TextView

lblNxtMessage.setId(View.generateViewId());

EXAMPLE

 public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final ConstraintLayout clMessagesContainer = (ConstraintLayout) findViewById(R.id.clMessagesContainer);
        final EditText txtMessage = (EditText) findViewById(R.id.txtMessage);
        Button btnSendL = (Button) findViewById(R.id.btnSendL);
        Button btnSendR = (Button) findViewById(R.id.btnSendR);

        btnSendL.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {

                TextView lblNxtMessage = new TextView(MainActivity.this);
                lblNxtMessage.setId(View.generateViewId());
                lblNxtMessage.setLayoutParams(new ConstraintLayout.LayoutParams(
                        ConstraintLayout.LayoutParams.WRAP_CONTENT,
                        ConstraintLayout.LayoutParams.WRAP_CONTENT));

                lblNxtMessage.setText(txtMessage.getText().toString());
                clMessagesContainer.addView(lblNxtMessage);

                ConstraintSet csL = new ConstraintSet();
                csL.clone(clMessagesContainer);
                csL.connect(lblNxtMessage.getId(), ConstraintSet.LEFT, ConstraintSet.PARENT_ID, ConstraintSet.LEFT, 0);
                csL.connect(lblNxtMessage.getId(), ConstraintSet.TOP, ConstraintSet.PARENT_ID, ConstraintSet.TOP, 0);
                csL.applyTo(clMessagesContainer);

            }
        });

        btnSendR.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {

                TextView lblNxtMessage = new TextView(MainActivity.this);
                lblNxtMessage.setId(View.generateViewId());
                lblNxtMessage.setLayoutParams(new ConstraintLayout.LayoutParams(
                        ConstraintLayout.LayoutParams.WRAP_CONTENT,
                        ConstraintLayout.LayoutParams.WRAP_CONTENT));

                lblNxtMessage.setText(txtMessage.getText().toString());
                clMessagesContainer.addView(lblNxtMessage);

                ConstraintSet csR = new ConstraintSet();
                csR.clone(clMessagesContainer);
                csR.connect(lblNxtMessage.getId(), ConstraintSet.RIGHT, ConstraintSet.PARENT_ID, ConstraintSet.RIGHT, 0);
                csR.connect(lblNxtMessage.getId(), ConstraintSet.TOP, ConstraintSet.PARENT_ID, ConstraintSet.TOP, 0);
                csR.applyTo(clMessagesContainer);
            }
        });
    }
}
AskNilesh
  • 67,701
  • 16
  • 123
  • 163
pistolcaffe
  • 849
  • 1
  • 8
  • 15