0

I have created two imageViews promatically as shown below:

        public void createImageViews(Integer count){



    ImageView[] imageViewArray = new ImageView[count];

    for (int i = 0; i < count; i++ ) {
        imageViewArray[i] = new ImageView(getBaseContext());
        imageViewArray[i].setId(i); // unique property for every imageView


        if(i==0){
            RelativeLayout.LayoutParams params =  new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);

            params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
            params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
            imageViewArray[i].setLayoutParams(params);
            imageViewArray[i].setBackgroundResource(imagesForIv[i]);
            _UIRLParent.addView(imageViewArray[i]);
            Log.v("first", "first"+i);
        }
        else if(i < 3){

            RelativeLayout.LayoutParams params =  new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
            params.addRule(RelativeLayout.RIGHT_OF,imageViewArray[i].getId());
            imageViewArray[i].setBackgroundResource(imagesForIv[i]);
            _UIRLParent.addView(imageViewArray[i],params);
            Log.v("second", "second"+i);
        }

    }

I just need to place the second imageView toRightOf first imageView. Can someone help me. This is eating away a lot of my time.

Vivekanand
  • 755
  • 1
  • 8
  • 29

1 Answers1

0

try https://stackoverflow.com/a/5191159/1436931

you are using wrong index values.

at line

params.addRule(RelativeLayout.RIGHT_OF,imageViewArray[i].getId());

you aligning current image RIGHT of current image. :)

you need to track the index of last imageView id i.e left Image view

you need something like this

params.addRule(RelativeLayout.RIGHT_OF,imageViewArray[i-1].getId());

Community
  • 1
  • 1
Mohsin Naeem
  • 12,542
  • 3
  • 39
  • 53