Android: How to Programmatically Set Layout_Constraintright_Torightof "Parent"

What is the equivalent of layout_constraintRight_toLeftOf programmatically in android?

You can set like this,

ConstraintSet constraintSet = new ConstraintSet();
constraintSet.clone(layout); // root layout

// If you want to align parent to the layout left
constraintSet.connect(textView.getId(), ConstraintSet.LEFT, layout.getId(), ConstraintSet.LEFT);

// If you want to align to the left of one more textview - second text view
constraintSet.connect(textView.getId(), ConstraintSet.RIGHT, textView1.getId(), ConstraintSet.LEFT);

/*______________________________
| Text1 Text2 |
| |*/
constraintSet.applyTo(layout);

How can i change constraint programmatically

First add:

android:id="@+id/constraint_layout"

to your ConstraintLayout, then change:

android:onClick="on_expand"

in:

android:onClick="onExpand"

to respect lowerCamelCase methods naming convention.

Finally this should be your code:

public class MainActivity extends AppCompatActivity {

private ConstraintLayout mConstraintLayout;
private Button mButton;
private ConstraintSet mConstraintSet = new ConstraintSet();

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mConstraintLayout = findViewById(R.id.constraint_layout);
mButton = findViewById(R.id.button2);
}

public void onExpand(View view) {
// If the button is already visible then you don't need to apply any constraints
if (mButton.getVisibility() == View.VISIBLE) {
return;
}
/* Otherwise:
- Make the button2 visible;
- Insert the actual constraints in the ConstraintSet
- Define a new constraint between button3 and button2 (TopToBottomOf)
- Apply it to the ConstraintLayout */
mButton.setVisibility(View.VISIBLE);
mConstraintSet.clone(mConstraintLayout);
mConstraintSet.connect(R.id.button3, ConstraintSet.TOP,
R.id.button2, ConstraintSet.BOTTOM);
mConstraintSet.applyTo(mConstraintLayout);
}
}

ConstraintLayout: change constraints programmatically

  1. To set constraints of image view to:

     app:layout_constraintRight_toRightOf="@+id/check_answer1"
    app:layout_constraintTop_toTopOf="@+id/check_answer1"

    use:

     ConstraintLayout constraintLayout = findViewById(R.id.parent_layout);
    ConstraintSet constraintSet = new ConstraintSet();
    constraintSet.clone(constraintLayout);
    constraintSet.connect(R.id.imageView,ConstraintSet.RIGHT,R.id.check_answer1,ConstraintSet.RIGHT,0);
    constraintSet.connect(R.id.imageView,ConstraintSet.TOP,R.id.check_answer1,ConstraintSet.TOP,0);
    constraintSet.applyTo(constraintLayout);
  2. To set constraints of image view to:

     app:layout_constraintRight_toRightOf="@+id/check_answer2"
    app:layout_constraintTop_toTopOf="@+id/check_answer2"

    use:

     ConstraintLayout constraintLayout = findViewById(R.id.parent_layout);
    ConstraintSet constraintSet = new ConstraintSet();
    constraintSet.clone(constraintLayout);
    constraintSet.connect(R.id.imageView,ConstraintSet.RIGHT,R.id.check_answer2,ConstraintSet.RIGHT,0);
    constraintSet.connect(R.id.imageView,ConstraintSet.TOP,R.id.check_answer2,ConstraintSet.TOP,0);
    constraintSet.applyTo(constraintLayout);

Changing width and height in ConstraintLayout programmatically doesn't work

You're doing it wrong. You've to apply a new ConstraintSet like:

val constraintSet1 = ConstraintSet()
constraintSet1.clone(yourConstraintLayout)

val constraintSet2 = ConstraintSet()
constraintSet2.clone(yourConstraintLayout)
constraintSet2.constrainWidth(R.id.one, 0)
constraintSet2.constrainWidth(R.id.two, 0)
constraintSet2.constrainHeight(R.id.one, 0)
constraintSet2.constrainHeight(R.id.two, 0)

// to switch to full screen
constraintSet2.applyTo(yourConstraintLayout)

// to get back to your original screen
constraintSet1.applyTo(yourConstraintLayout)

Android ConstraintLayout: set one element in the center of another one

If you have to set it in the center just change the constraints of the image with :

 app:layout_constraintBottom_toBottomOf="@id/login_next_btn"
app:layout_constraintTop_toTopOf="@id/login_next_btn"
app:layout_constraintStart_toStartOf="@id/login_next_btn"
app:layout_constraintEnd_toEndOf="@id/login_next_btn"


Related Topics



Leave a reply



Submit