1

I have a constraint layout which consists of 2 static textviews and a few buttons added programmatically. I want to create a chain with all that buttons but I can't get it working properly as a few buttons are stuck to middle-bottom of the layout.

I will provide code below and maybe someone can give me a hint about what I am doing wrong.

 protected void onPostExecute(JSONArray result){
    super.onPostExecute(result);
        try {
            ConstraintLayout cl = (ConstraintLayout) findViewById(R.id.menu);
            ConstraintSet set = new ConstraintSet();
            List<Integer> chainIDs = new ArrayList<Integer>();
            int medId;
            for(int i=0; i < result.length() ; ++i)
            {
                if(i<1){
                    JSONObject json = result.getJSONObject(i);
                    Button btn = new Button(Menu.this);
                    btn.setId(i);
                    int startID = btn.getId();
                    btn.setText(json.getString("nume"));
                    btn.setBackgroundColor(getResources().getColor(R.color.yellow));
                    int width = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,153,getResources().getDisplayMetrics());
                    int height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,43,getResources().getDisplayMetrics());
                    int margins = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,8,getResources().getDisplayMetrics());
                    ConstraintLayout.LayoutParams lp = new ConstraintLayout.LayoutParams(width,height);
                    lp.setMargins(margins,margins,margins,margins);
                    cl.addView(btn, lp);
                    set.clone(cl);
                    set.connect(startID, ConstraintSet.LEFT,ConstraintSet.PARENT_ID , ConstraintSet.LEFT, 0);
                    set.connect(startID, ConstraintSet.RIGHT,ConstraintSet.PARENT_ID, ConstraintSet.RIGHT, 0);
                    Button btn1 =  findViewById(startID);
                    btn1.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Toast.makeText(Menu.this,"Apasare button", Toast.LENGTH_LONG).show();
                        }
                    });
                    chainIDs.add(startID);
                    set.applyTo(cl);
                    Log.d("STATE",Integer.toString(i));
                }else
                    {
                    JSONObject json = result.getJSONObject(i);
                    Button btn = new Button(Menu.this);
                    btn.setId(i);
                    medId = btn.getId();
                    btn.setText(json.getString("nume"));
                    btn.setBackgroundColor(getResources().getColor(R.color.yellow));
                    int width = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,153,getResources().getDisplayMetrics());
                    int height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,43,getResources().getDisplayMetrics());
                    int margins = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,8,getResources().getDisplayMetrics());
                    ConstraintLayout.LayoutParams lp = new ConstraintLayout.LayoutParams(width,height);
                    lp.setMargins(margins,margins,margins,margins);
                    cl.addView(btn, lp);
                    set.clone(cl);
                    set.connect(medId, ConstraintSet.LEFT,ConstraintSet.PARENT_ID , ConstraintSet.LEFT, 0);
                    set.connect(medId, ConstraintSet.RIGHT,ConstraintSet.PARENT_ID, ConstraintSet.RIGHT, 0);
                    set.applyTo(cl);
                    Button btn1 =  findViewById(medId);
                    btn1.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Toast.makeText(Menu.this,"Apasare button", Toast.LENGTH_LONG).show();
                        }
                    });
                    chainIDs.add(medId);
                    Log.d("STATE",Integer.toString(i));
                }

            }
            int[] chain = new  int [chainIDs.size()];
            Iterator<Integer> iterator = chainIDs.iterator();
            for (int j = 0; j < chain.length; j++)
            {
                chain[j] = iterator.next().intValue();
            }
            set.createVerticalChain(R.id.descriere,ConstraintSet.BOTTOM,ConstraintSet.PARENT_ID,ConstraintSet.BOTTOM,chain,null, ConstraintSet.CHAIN_SPREAD);
            set.applyTo(cl);
            setContentView(cl);
        }
        catch(Exception ex){
            ex.printStackTrace();
        }
    }
}

Here is the actual output: https://i.stack.imgur.com/tXnQS.jpg

The desired output would look similar to:

start layout
textview
button 1
button 2
.
.
.
button n
end of layout
EDIT Solved after using View.generateViewId() method

Cœur
  • 37,241
  • 25
  • 195
  • 267
  • @Cheticamp I put 0 cause I saw it worked in other examples and by the way, it takes changes if I apply any to that particular control so I don't think that's the problem. I updated my post and check now how the code looks like, also `createVerticalChain()` method is updated. –  Jan 06 '19 at 21:49
  • @Cheticamp Please post answer so I can approve as the right answer to my question.Thank you –  Jan 06 '19 at 22:15
  • @Cheticamp In a few words I only used `generateViewId()` to generate Id's of controls and changed parameters to `createVerticalChain()` method. –  Jan 06 '19 at 22:58

1 Answers1

1

Zero is not a valid view id. See generateViewId() for how to generate valid ids.

Also check that you have the right arguments for createVerticalChain(). The second argument should be a side not an id.

Cheticamp
  • 61,413
  • 10
  • 78
  • 131