Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to MODIFY a constraint layout programmatically?

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 ?

like image 752
Amir Ziarati Avatar asked Aug 28 '17 13:08

Amir Ziarati


People also ask

How do I change constraint layout?

Open the layout file (activity_main. xml) in Android Studio and click the Design tab at the bottom of the editor window. In the Component Tree window, right-click LinearLayout and then choose Convert layout to ConstraintLayout from the context menu.

How do you set margin programmatically in constraint layout?

WRAP_CONTENT); ConstraintLayout. MarginLayoutParams layoutParams = new ConstraintLayout. MarginLayoutParams(newLayoutParams); layoutParams. setMargins(0, 0, 0, 0); toolbar.


1 Answers

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

like image 56
Re'em Avatar answered Sep 20 '22 19:09

Re'em