Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i change constraint programmatically

I need some help with ConstraintLayout.

I have 3 Buttons vertically aligned. Button1 and Button3 are visible, Button2 is invisible. When I click on Button1, Button2 should be visible.

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="8dp"
    android:onClick="on_expand"
    android:text="Button"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/textView" />

<Button
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="0dp"
    android:layout_marginTop="8dp"
    android:text="Button"
    android:visibility="invisible"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/button1" />

<Button
    android:id="@+id/button3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="8dp"
    android:text="Button"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toBottomOf="@id/button1" />

How can I set

app:layout_constraintTop_toBottomOf="@id/button2"

on Button3

I have found this

Button b1 = (Button)findViewById(R.id.button1);
Button b2 = (Button) findViewById(R.id.button2);
Button b3 = (Button) findViewById(R.id.button3);    

ConstraintSet constraintSet = new ConstraintSet();
        constraintSet.connect("What must i set here????");
        constraintSet.clone(mConstraintLayout);
        constraintSet.applyTo(mConstraintLayout);
like image 796
mebus08 Avatar asked Dec 31 '17 14:12

mebus08


People also ask

How do I change a constraint in Swift?

Select the height constraint from the Interface builder and take an outlet of it. So, when you want to change the height of the view you can use the below code. Method updateConstraints() is an instance method of UIView . It is helpful when you are setting the constraints programmatically.

How do I give a constraint to button programmatically in Swift?

Add the button in the view, give it constraints and as you are using constraints, you can skip the button. frame and add widthAnchor and heightAnchor . At last activate them and keep translatesAutoresizingMaskIntoConstraints as false . Also, it will be better if you can add proper names.


1 Answers

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);
    }
}
like image 89
Sal Avatar answered Oct 16 '22 02:10

Sal