2

i have couple of imageViews that i create programmatically, so here is my question, if i want to put one imageView on the other, how can i tell my app which imageView is going to be on top, and which one on the bottom ? This is how i create ImageViews, and that works fine.

        image = new ImageView(this);

    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.WRAP_CONTENT,
            RelativeLayout.LayoutParams.WRAP_CONTENT);

    image.setScaleType(ImageView.ScaleType.MATRIX);
    image.setImageResource(R.drawable.ic_launcher);
    image.setId(1);
         params.setMargins(0, 0, 0, 0);
     Let's get the root layout and add our ImageView

    ((ViewGroup) mainLayout).addView(image, params);
Mate Križanac
  • 268
  • 1
  • 7
  • 18
  • Very similar to http://stackoverflow.com/questions/4182486/placing-overlappingz-index-a-view-above-another-view-in-android – luanjot Oct 14 '13 at 20:04

2 Answers2

4

So this is what helped me in the end, i am gonna put it so it may be of use to someone else.

You can use this: http://developer.android.com/reference/android/view/View.html#bringToFront%28%29

Just use it like this

YourImageView.bringToFront();

After you created both of the images and you need to put one on top. This works for most Views.

Mate Križanac
  • 268
  • 1
  • 7
  • 18
2

If you want to draw ImageViews on top of each other then you should use a FrameLayout. This is a layout that draws views like a stack, the first child will be drawn first and others will be drawn on top of the previous. For example:

<FrameLayout
 android:layout_width="match_parent"
 android:layout_height="match_parent">
    <ImageView1 ... />
    <ImageView2 ... />
</FrameLayout>

If your layout is like this, and ImageView1 and ImageView2 overlap, then ImageView2 will be drawn over ImageView1 because it is the second child and is drawn last.

telkins
  • 10,440
  • 8
  • 52
  • 79
  • but i create my imageViews from code, not layout, thats why i can't do it with FrameLyout – Mate Križanac Oct 14 '13 at 22:28
  • is that possible? i didn't know i can use FrameLayout in code. Sorry, i am still a beginner. But i found a way to do this easy via code. I am gonna put my answer below. – Mate Križanac Oct 15 '13 at 12:21
  • @MateKrižanac A FrameLayout is simply a subclass of ViewGroup, so of course you can. :) Check out this link under the heading "Define a FrameLayout Programmatically": http://mobile.tutsplus.com/tutorials/android/android-sdk_frame-layout/ – telkins Oct 15 '13 at 13:52