0

I need to place multiple new ImageViews in my layout. The problem is that one comes on top of another exactly at the same location. Although I change the location, it only relates to the first one. They both are at 80,80.

RelativeLayout.LayoutParams lp1 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);

    addpPicD(lp1,80,80);
    addpPicD(lp1,100,100);
}

private void addpPicD(LayoutParams lp, int Lan, int Lon) 
{

        lp.setMargins(Lan, Lon, 0, 0);
        ImageView imageView = new ImageView(this);  
        imageView.setImageResource(R.drawable.dot_bl);  
        imageView.setLayoutParams(lp);
        rel.addView(imageView);

}
Dim
  • 4,527
  • 15
  • 80
  • 139

2 Answers2

3

RelativeLayout has its own Layout Parameters. To place child views side by side, or vertically you will need to provide rules. Use addRule() on layout parameters of each view you add.

Pass values like RelativeLayout.BELOW, RelativeLayout.RIGHT_OF etc along with the id of view with which you want to align with.

S.D.
  • 29,290
  • 3
  • 79
  • 130
1

Your problem is that you set the layoutparams the first time you create the layout and then they aren't recreated as you would think they would be.

Simple solution. Change it to the code below which works as tested by me:

RelativeLayout.LayoutParams lp1 = null;

addpPicD(lp1,80,80);
addpPicD(lp1,100,100);
}

private void addpPicD(LayoutParams lp, int Lan, int Lon) 
{
    lp = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    lp.setMargins(Lan, Lon, 0, 0);
    ImageView imageView = new ImageView(this);  
    imageView.setImageResource(R.drawable.dot_bl);  
    imageView.setLayoutParams(lp);
    rel.addView(imageView);

}
AndroidPenguin
  • 3,445
  • 2
  • 21
  • 42