38

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 ?

4 Answers 4

55

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

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks a lot man. You just helped a lot. I was having issue settings toptotop of a view. You method worked flawlessly.
Note: It is completely necessary to call setLayoutParams(). Some times, it works even if you don't call it, but not this one.
Awesome, you saved me
That is the best answer. for some reason calling requestLayout not working
48

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

someView.updateLayoutParams<ConstraintLayout.LayoutParams> { horizontalBias = 0.5f }

2 Comments

tnx sir ... very quick and useful 👌👌👌👌
this is the answer, so glad I found.
14

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));

Comments

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
    }
}

3 Comments

this is not what i want; i just want to change bias only. actually its toggling between two state !!! not my answer.
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.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.