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.sstatic.net/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
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.