Android: how to programmatically set layout_constraintRight_toRightOf "parent"

I have a view in ConstrainLayout as follows.

<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:maxWidth="260dp" android:textColor="#FFF" android:textSize="16sp" app:layout_constraintLeft_toLeftOf="@+id/parent" app:layout_constraintTop_toBottomOf="@id/message_date" android:id="@+id/text_main" /> 

I would like to change the view to app:layout_constraintLeft_toLeftOf="@+id/parent" or layout_constraintLeft_toRightOf="@+id/parent" programmatically in recycleViewHolder based on some conditions.

+8
android android-support-library android-constraintlayout
source share
1 answer

Here is an example of setting a button at the bottom of the parent view using java code:

 ConstraintLayout constraintLayout; ConstraintSet constraintSet; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); constraintLayout = (ConstraintLayout) findViewById(R.id.activity_main_constraint_layout); Button button = new Button(this); button.setText("Hello"); constraintLayout.addView(button); constraintSet = new ConstraintSet(); constraintSet.clone(constraintLayout); constraintSet.connect(button.getId(), ConstraintSet.LEFT, constraintLayout.getId(), ConstraintSet.RIGHT, 0); constraintSet.constrainDefaultHeight(button.getId(), 200); constraintSet.applyTo(constraintLayout); } 

to achieve something like this

 app:layout_constraintLeft_toLeftOf="@+id/parent" 

your Java code should look like this:

 set.connect(YOURVIEW.getId(),ConstraintSet.LEFT,ConstraintSet.PARENT_ID,ConstraintSet.LEFT,0); 

and to achieve something like that,

 layout_constraintLeft_toRightOf="@+id/parent" 

your Java code should look like this:

 set.connect(YOURVIEW.getId(),ConstraintSet.LEFT,ConstraintSet.PARENT_ID,ConstraintSet.RIGHT,0); 

Here I assume that android:id="@+id/parent" is the identifier of your parent ConstraintLayout.

+12
source share

All Articles