Moritz
Moritz

Reputation: 159

Change the app:layout_constraintHorizontal_bias="" in the ConstraintLayout

In my XML-File I have a ConstaintLayout. There I have a ImageView with this settings:

<ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="1"
            app:layout_constraintStart_toEndOf="@+id/game_square"
            app:layout_constraintTop_toTopOf="parent"
            app:srcCompat="@mipmap/ic_game_quadrat"
            android:visibility="invisible"
            android:id="@+id/game_right"/>

How can I access in the java file on the app:layout_constraintHorizontal_bias="1" to change it to for example 0.

Moritz

Upvotes: 6

Views: 8291

Answers (4)

Goku
Goku

Reputation: 9732

Change the app:layout_constraintHorizontal_bias=“” in the ConstraintLayout

Than Use ConstraintSet

This class allows you to define programmatically a set of constraints to be used with ConstraintLayout. It lets you create and save constraints, and apply them to an existing ConstraintLayout. ConstraintsSet can be created in various ways:

CODE

    ConstraintSet set = new ConstraintSet();
    ImageView view = (ImageView)findViewById(R.id.game_right);
    ConstraintLayout constraintLayout = (ConstraintLayout)findViewById(R.id.activity_constraint);
    set.clone(constraintLayout);
    set.setHorizontalBias(view,0);
    set.applyTo(constraintLayout);

Upvotes: 4

Achraf Amil
Achraf Amil

Reputation: 1375

In Kotlin I use something like

fun ConstraintLayout.setHorizontalBias(
    @IdRes targetViewId: Int,
    bias: Float
) {
    val constraintSet = ConstraintSet()
    constraintSet.clone(this)
    constraintSet.setHorizontalBias(targetViewId, bias)
    constraintSet.applyTo(this)
}

Upvotes: 3

Moritz
Moritz

Reputation: 159

After change a little bit the other anwsers I find a solution:

ConstraintLayout cl = (ConstraintLayout) findViewById(R.id.activity_constraint);
ConstraintSet cs = new ConstraintSet();
cs.clone(cl);
cs.setHorizontalBias(R.id.game_right, (float) 0);
cs.applyTo(cl);

Moritz

Upvotes: 7

Yuliwee
Yuliwee

Reputation: 337

You can try something like

ConstraintSet constraintSet = new ConstraintSet();
constraintSet.clone(context, R.id.activity_constraint);
float biasedValue = 0f;
constraintSet.setHorizontalBias(R.id.game_right, biasedValue);
constraintSet.applyTo((ConstraintLayout) findViewById(R.id.activity_constraint));

Upvotes: 13

Related Questions