10

I want to add 2 buttons to a ConstraintLayout. My current code is as following:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ConstraintLayout layout = (ConstraintLayout) findViewById(R.id.activity_main);
    ConstraintSet set = new ConstraintSet();
    set.clone(layout);

    //Button 1: 
    Button button = new Button(this);
    button.setText("Hello");
    layout.addView(button);

    set.connect(button.getId(), ConstraintSet.LEFT, layout.getId(), ConstraintSet.LEFT, 0);
    set.connect(button.getId(), ConstraintSet.RIGHT, layout.getId(), ConstraintSet.RIGHT, 0);
    set.connect(button.getId(), ConstraintSet.BOTTOM, layout.getId(), ConstraintSet.BOTTOM, 0);
    set.constrainWidth(button.getId(), ConstraintSet.MATCH_CONSTRAINT);
    set.constrainHeight(button.getId(), 200);
    set.applyTo(layout);


    //Button 2:     
    Button newButton = new Button(this);
    newButton.setText("Yeeey");
    layout.addView(newButton);

    set.connect(newButton.getId(), ConstraintSet.BOTTOM, button.getId(), ConstraintSet.TOP, 0);
    set.connect(newButton.getId(), ConstraintSet.LEFT, button.getId(), ConstraintSet.LEFT, 0);
    set.connect(newButton.getId(), ConstraintSet.RIGHT, button.getId(), ConstraintSet.RIGHT, 0);
    set.constrainHeight(newButton.getId(), 200);
    set.applyTo(layout);

}

But I only get 1 visible button (the other is probably hidden behind this one), and it's in the top left corner of the screen. There's supposed to be 2 buttons, at the bottom of the screen, linked to each other.

What am I doing wrong here?

enter image description here

Desired outcome:

enter image description here

1 Answer 1

28

Here is the working code of what you want to achieve

  @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ConstraintLayout layout = (ConstraintLayout) findViewById(R.id.activity_main);
        ConstraintSet set = new ConstraintSet();
        set.clone(layout);

        //Button 1:
        Button button = new Button(this);
        button.setText("Hello");
        button.setId(100);           // <-- Important
        layout.addView(button);
        set.connect(button.getId(), ConstraintSet.BOTTOM, ConstraintSet.PARENT_ID, ConstraintSet.BOTTOM, 0);
        set.connect(button.getId(),ConstraintSet.RIGHT,ConstraintSet.PARENT_ID,ConstraintSet.RIGHT,0);
        set.connect(button.getId(),ConstraintSet.LEFT,ConstraintSet.PARENT_ID,ConstraintSet.LEFT,0);
        set.constrainHeight(button.getId(), 200);
        set.applyTo(layout);


        //Button 2:
        Button newButton = new Button(this);
        newButton.setText("Yeeey");
        layout.addView(newButton);
        set.connect(newButton.getId(), ConstraintSet.BOTTOM, button.getId(), ConstraintSet.TOP, 0);
        set.connect(newButton.getId(),ConstraintSet.RIGHT,ConstraintSet.PARENT_ID,ConstraintSet.RIGHT,0);
        set.connect(newButton.getId(),ConstraintSet.LEFT,ConstraintSet.PARENT_ID,ConstraintSet.LEFT,0);
        set.constrainHeight(newButton.getId(), 200);
        set.applyTo(layout);


    }

Important:
If id is not set explicitly, all the elements will get the same id(-1).

7
  • Great. This is working code. I can see you've added "button.setID(100);" and changed "layout.getId()" to "ConstraintSet.PARENT_ID". Could you explain to me why this made it work?
    – Humilton
    Commented Jan 16, 2017 at 8:06
  • 6
    I see. Thank you. For others reading these comments: I printed the content of .getId() for each button. If the .setId() is not used then it just defaults the ID to -1. Which means all your buttons have an ID of -1. In that case, your set.connect()'s will not be able to distinguish from one button to another.
    – Humilton
    Commented Jan 16, 2017 at 8:21
  • 1
    @Einar this explanation really helped me. The setId was key, to fixing my issues. Commented Oct 6, 2017 at 7:01
  • 2
    Einar's comment really helps, thus added to answer. Commented Jan 17, 2018 at 6:28
  • 6
    you should use View.generateViewId() instead of using an literal int as argument.
    – user8389458
    Commented Apr 24, 2018 at 14:20

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.