How to utilise seperate ConstraintSet actions for different TextViews in the same ConstraintLayout?
How to utilise seperate ConstraintSet actions for different TextViews in the same ConstraintLayout?
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 Answers
2
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
.
View
ID
-1
Views
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:
ID
View
generateViewId()
View
TextView lblNxtMessage = new TextView(MainActivity.this);
lblNxtMessage.setId(View.generateViewId());
I recommend setId()
to new TextView
setId()
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);
}
});
}
}
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Comments
Post a Comment