how to MODIFY a constraint layout programmatically?

Amir Ziarati picture Amir Ziarati · Aug 28, 2017 · Viewed 12.1k times · Source

Ive got a view like below :

 <org.rayanmehr.atlas.shared.customview.CustomTextView
    android:id="@+id/tvDownVoteCount"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="6dp"
    app:layout_constraintVertical_bias="0"
    app:layout_constraintTop_toBottomOf="@+id/anchorHelper"
    app:layout_constraintLeft_toRightOf="@+id/tvDownVoteIcon"
    app:layout_constraintBottom_toBottomOf="@+id/imgComment"
    />

how can i modify the value of app:layout_constraintVertical_bias or any other constraint property programmatically without having top set the whole set of the properties again in my activity ?

Answer

Re&#39;em picture Re'em · Jan 9, 2018

Here is what I did (no need for ConstraintSet, we can work directly on the constraints themselves):

ConstraintLayout.LayoutParams params = (ConstraintLayout.LayoutParams) myView.getLayoutParams();
params.horizontalBias = 0.2f; // here is one modification for example. modify anything else you want :)
myView.setLayoutParams(params); // request the view to use the new modified params

it worked like a charm when I had a SeekBar and a TextView below it (aligned to it both left+right), and I wanted to update the TextView position to be under the SeekBar's cursor, so I had to update the horizontal bias params on the SeekBar's OnSeekBarChangeListener