3

I am trying to have two images on tp of each other. I can have it work fine with an xml file but I would like to do this dynamically. ctdeasyone is a transparent image.

So this works fine..

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <ImageView 
        android:id="@+id/bck1" 
        android:src="@drawable/fish2"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"        
        android:scaleType="fitXY" 
        android:layout_gravity="center">
    </ImageView>

    <ImageView 
        android:id="@+id/bck2" 
        android:src="@drawable/ctdeasyone"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"        
        android:scaleType="fitXY" 
        android:layout_gravity="center">
    </ImageView>

</RelativeLayout>

When I do this. only the second image shows up (it is the transparent one.) Can any of the experts advice on this? Newbbie here... This is my first question. TIA.

public class TwoPicksOnEachOther extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Grabbing the Application context         
        final Context context = getApplication();                   

        RelativeLayout relativeLayout = new RelativeLayout(this);                   

        final ImageView iv = new ImageView(this);         
        iv.setImageResource(R.drawable.fish2);


        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(     
                RelativeLayout.LayoutParams.FILL_PARENT, 
                RelativeLayout.LayoutParams.FILL_PARENT);
        relativeLayout.addView(iv,lp);        

        // Creating transparent image
        final ImageView iv2 = new ImageView(this);
        iv.setImageResource(R.drawable.ctdeasytwo);
        RelativeLayout.LayoutParams lp2 = new RelativeLayout.LayoutParams(     
                RelativeLayout.LayoutParams.FILL_PARENT, 
                RelativeLayout.LayoutParams.FILL_PARENT);
        relativeLayout.addView(iv2,lp2);
        setContentView(relativeLayout);

    }        

}

2 Answers 2

3

I had to put it in the emulator and play with it for a while until I saw it:

iv.setImageResource(R.drawable.fish2);
(...)
iv.setImageResource(R.drawable.ctdeasytwo);

You're never setting the image resource for iv2!

I changed that and now I see two images as expected.

Sign up to request clarification or add additional context in comments.

4 Comments

Aleadam, I am trying to vote your answer up but it says I need 15 reputations. What a bug! I am setting iv.setImageResource twice. Never noticed. Works fine now. Thanks a lot.
@George yes, you new users can accept answers but not upvote. It took me a while to figure that bug out also!
maybe there is someting I don't get. I was suposed to get 5 reputations for voting up, but I can't vote up becasue I need 15 reputations. So how do you get these reputations??? When I figure it out, I will come back to vote you so you can get your 10.
You get +5 for upvotes from others on your won questions, or +10 for upvotes on your own answers. I upvoted now your question because it's well-written, with a clear problem. Good luck!
0

This is perfect for FrameLayout . Since you want the images right on top of each other.

2 Comments

Even though the xml works perfect with relative layout you are suggesting to do this with FrameLoyout? I will try this if no else can help with the code above. I mean, it works with xml why not dynamically?
Just reiewed the FrameLyout you suggested. Very intersting as you suggested. I will try as sson as kids go to bed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.