Android : How to programmatically set layout_constraintRight_toRightOf "parent"

AndroidAndroid Support-LibraryAndroid Constraintlayout

Android Problem Overview


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 either app:layout_constraintLeft_toLeftOf="@+id/parent" or layout_constraintLeft_toRightOf="@+id/parent" programmatically in recycleViewHolder based on some conditions.

Android Solutions


Solution 1 - Android

Here is an example of setting a button to the bottom of 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 this,

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'm assuming android:id="@+id/parent" is the id of your parent ConstraintLayout .

Solution 2 - Android

constraintSet = new ConstraintSet(); and so on

Did not work for me anyhow.

Solved by

  LayoutParams layoutParams = (LayoutParams) viewToChange.getLayoutParams();
    layoutParams.leftToLeft = anotherViewId;
    layoutParams.rightToRight =anotherViewId;
    layoutParams.topToTop = anotherViewId;
    layoutParams.bottomToBottom = anotherViewId;
    layoutParams.startToStart =anotherViewId;
    layoutParams.endToEnd = anotherViewId;
    viewToChange.setLayoutParams(layoutParams);

Solution 3 - Android

Use id

class MyLayout(context: Context) : ConstraintLayout(context) {

    fun show() {
        val view = ImageView(context)
        addView(view)
        val params = view.layoutParams as ConstraintLayout.LayoutParams
        params.height = 100 
        params.width = 50
        params.rightToRight = id
        view.requestLayout()
    }
}

Solution 4 - Android

I had same situation when I needed to move buttons down once a new EditText appears, In XML file they had constraints like this:

app:layout_constraintTop_toBottomOf="@+id/first_layout"

and what I needed to do is changing to sth like programmatically:

app:layout_constraintTop_toBottomOf="@+id/second_layout"

Using ConstraintLayout.LayoutParams the task is pretty straightforward e.g.

Kotlin:

val currentLayout = btn.layoutParams as ConstraintLayout.LayoutParams // btn is a View here
currentLayout.topToBottom = R.id.second_layout // resource ID of new parent field
btn.layoutParams = currentLayout

and that's it!

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRelmView Question on Stackoverflow
Solution 1 - AndroidDarshan SoniView Answer on Stackoverflow
Solution 2 - AndroidHoliday FunView Answer on Stackoverflow
Solution 3 - Androidonmyway133View Answer on Stackoverflow
Solution 4 - AndroiddsoundedView Answer on Stackoverflow