Reputation: 15
I set my layout programmatically because I fill it depending of how much images I get from a request. What I want to do is create two textview at the right of the image, the second below the first. With what I found out on other answers, I end out with my second textview to be overlapping the image and is not below the other textview.
int counter = 0;
while(mList.size()!=counter)
{
RelativeLayout row = new RelativeLayout(context);
row.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
ImageButton imgBtn = new ImageButton(context);
Bitmap image = getImage(mList.get(counter));
imgBtn.setImageBitmap(image);
imgBtn.setId(counter + 1); // Because it need to be a positive integer.
imgBtn.setBackground(null);
imgBtn.setPadding(0, 0, 0, 0);
row.addView(imgBtn);
TextView firstText = new TextView(context);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.RIGHT_OF, imgBtn.getId());
lp.addRule(RelativeLayout.CENTER_IN_PARENT);
firstText.setLayoutParams(lp);
firstText.setId(counter+100);
row.addView(firstText);
TextView secondText = new TextView(context);
RelativeLayout.LayoutParams lp2 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
lp2.addRule(RelativeLayout.BELOW,firstText.getId());
secondText.setLayoutParams(lp2);
row.addView(secondText);
++counter;
linlayout.addView(row);
}
Upvotes: 0
Views: 439
Reputation: 5542
USe linearLayout instead of relative.In this you won't be needing to set counter and id. Instead setTag to the view.To make code more readable.
You can just set the orientation of the row layout and then no need to bother about other params to position the view.
I am not adding the code coz i know you can write it very well ,just change the approach.
Upvotes: 1