0

I need to overlap images one image by another. Below is my code

int cards[] = {R.drawable.c1,R.drawable.c2,R.drawable.c3,R.drawable.c4,R.drawable.c5,R.drawable.c6,
            R.drawable.c7,R.drawable.c8,R.drawable.c9,R.drawable.c10,R.drawable.c11,R.drawable.c12,R.drawable.c13};

    ImageView[] Images = new ImageView[cards.length];
        for (int i = 0; i < cards.length; i++) {           
            Images[i] = new ImageView(this);

            Images[i].setImageBitmap(BitmapFactory.decodeResource(getResources(), cards[i]));
            Images[i].setId(i);
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                    RelativeLayout.LayoutParams.WRAP_CONTENT,
                    RelativeLayout.LayoutParams.WRAP_CONTENT);
            if (i != 0) {                
                params.addRule(RelativeLayout.RIGHT_OF,Images[i - 1].getId());
              params.addRule(RelativeLayout.ALIGN_PARENT_TOP, Images[i - 1].getId());
              layout.addView(Images[i], params);
            } else {
                params.addRule(RelativeLayout.LEFT_OF,Images[0].getId());
                params.rightMargin=10;
                layout.addView(Images[i]);
            }

       }

With this i am not able to display first image and remaining images are displayed but not overlapped with other images. How to fix this?

Vamshi
  • 1,495
  • 1
  • 15
  • 31

2 Answers2

0

Finally able to fix the issue with help of this link

ImageView[] Images = new ImageView[cards.length];
        for (int i = 0; i < cards.length; i++) {           
            Images[i] = new ImageView(this);

            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                    RelativeLayout.LayoutParams.WRAP_CONTENT,
                    RelativeLayout.LayoutParams.WRAP_CONTENT);
            if (i != 0) {          
                 params.addRule(RelativeLayout.ALIGN_TOP, i-1);
                 params.addRule(RelativeLayout.ALIGN_LEFT,i-1);
                 params.leftMargin= 40;
                 Images[i].setImageBitmap(BitmapFactory.decodeResource(getResources(), cards[i]));
                 Images[i].setId(i);
                layout.addView(Images[i], params);
            } else {
                 Images[i].setImageBitmap(BitmapFactory.decodeResource(getResources(), cards[i]));
                 Images[i].setId(i);
                layout.addView(Images[i], params);
            }

        }
Community
  • 1
  • 1
Vamshi
  • 1,495
  • 1
  • 15
  • 31
-1

The code is totally wrong :) do you know what RIGHT_OF and LEFT_OF mean? just remove all the if else part and you should see all te images one on top of the other. Btw there are better ways to stack images on android like the LayerDrawable class.

Pasquale Anatriello
  • 2,355
  • 1
  • 16
  • 16
  • I am very much aware of those things, if i remove if else part we can see only one image...better to give the answers correctly rather than wrong suggestions and answers... – Vamshi Feb 10 '14 at 12:05
  • What do you want to achieve? It's not really clear from the image. You say that your images are not overlapped but your code tries to actually put them one next to the other – Pasquale Anatriello Feb 10 '14 at 12:11
  • Yes with the above code 1st image is not appearing on the screen.... and i am able to display the images side by side but i want them to overlap like 2nd image on 1st and 3rd image on 2nd so on.... – Vamshi Feb 10 '14 at 12:16
  • with overlap you mean show one on top of the other? – Pasquale Anatriello Feb 10 '14 at 15:52