35

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 ?

Amir Ziarati
  • 14,248
  • 11
  • 47
  • 52

4 Answers4

50

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

Edric
  • 24,639
  • 13
  • 81
  • 91
Re'em
  • 1,869
  • 1
  • 22
  • 28
39

If you are using Kotlin and you have androidx-core-ktx lib you can simply do:

someView.updateLayoutParams<ConstraintLayout.LayoutParams> { horizontalBias = 0.5f }
Ashley Figueira
  • 594
  • 4
  • 6
13

I just found the answer in here and you can use ConstraintSet to achieve this like below:

ConstraintSet constraintSet = new ConstraintSet();
constraintSet.clone(context, R.id.activity_constraint);

//for example lets change the vertical bias of tvDownVoteIcon
float biasedValue = 0.2f;
constraintSet.setVerticalBias(R.id.tvDownVoteIcon, biasedValue);

//or change the anchor
constraintSet.connect
(R.id.tvDownVoteIcon,ConstraintSet.RIGHT,R.id.txt,ConstraintSet.RIGHT,0);

//then apply
constraintSet.applyTo( (ConstraintLayout) findViewById(R.id.activity_constraint));
Amir Ziarati
  • 14,248
  • 11
  • 47
  • 52
0

Disclaimer: I haven't used this specific functionality. This is just my interpretation on how I would try to do it.

I think that what you need is ConstraintSet. After obtaining it, you could modify it and apply it again. This is a relevant example from this article.

override fun onCreate(savedInstanceState: Bundle?) {
    ...
    val constraintSet1 = ConstraintSet()
    constraintSet1.clone(constraintLayout)
    val constraintSet2 = ConstraintSet()
    constraintSet2.clone(constraintLayout)
    constraintSet2.centerVertically(R.id.image, 0)

    var changed = false
    findViewById(R.id.button).setOnClickListener {
        TransitionManager.beginDelayedTransition(constraintLayout)
        val constraint = if (changed) constraintSet1 else constraintSet2
        constraint.applyTo(constraintLayout)
        changed = !changed
    }
}
Xavier Rubio Jansana
  • 6,388
  • 1
  • 27
  • 50
  • this is not what i want; i just want to change bias only. actually its toggling between two state !!! not my answer. – Amir Ziarati Aug 28 '17 at 13:43
  • `ConstraintLayout` bases the layout in constraints, which are stored in a `ConstraintSet`. So, you need to alter a `ConstraintSet` in order to modify the bias, you cannot modify the bias directly. Of course, the example that I copied is not modifying specifically the bias, but that's the structure you should follow. – Xavier Rubio Jansana Aug 28 '17 at 13:45
  • 1
    yes you are true but thats not good to answer questions like this which will make the person who asks more confused. i found the answer myself. anyway thanks a lot to help. – Amir Ziarati Aug 28 '17 at 13:49