grgvn
grgvn

Reputation: 1329

How display some ImageView in a RelativeLayout?

I have a RelativeLayout

RelativeLayout myLayout = new RelativeLayout(this);

Then, I add 2 ImageView in this Layout:

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

ImageView img1 = new ImageView(this);
// Here I give the position of img1

ImageView img2 = new ImageView(this);
// And here the position and img2

myLayout.addView(img1, params);
myLayout.addView(img2, params);
setContentView(myLayout);

And here I have a problem: I want to show and click on the 2 ImageViews, but only the img2 is visible on my screen.

Is there a problem with the z-index or something else?

Upvotes: 0

Views: 2519

Answers (1)

Anju
Anju

Reputation: 9479

Since you are using RelativeLayout and same params for both ImageView one will be overlapped with the other. So define params for both. Add rule to each param for positioning it. And then give addView().

For eg:

RelativeLayout myLayout = new RelativeLayout(this);

ImageView img1 = new ImageView(this);
RelativeLayout.LayoutParams firstImageParams = new RelativeLayout.LayoutParams(
android.view.ViewGroup.LayoutParams.WRAP_CONTENT,
android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
leftArrowParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);

ImageView img2 = new ImageView(this);
RelativeLayout.LayoutParams secImageParams = new RelativeLayout.LayoutParams(
android.view.ViewGroup.LayoutParams.WRAP_CONTENT,android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
rightArrowParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);

myLayout.addView(img1, firstImageParams);
myLayout.addView(img2, secImageParams);
setContentView(myLayout);

Upvotes: 3

Related Questions