user1431282
user1431282

Reputation: 6835

Stacking 2 Images Android

I have been debugging this issue for the past 6 hours and have simplified my issue to the most simple case (taking out the bulk of my logic). I am trying to stack 2 (chickens) images on top of each other in a view.

I have followed numerous examples including the last answer to : Draw multiple bitmap on a view in android

My activity looks like:

public class FinalDisplay extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_final_display);

    LinearLayout layout = (LinearLayout) findViewById(R.id.activity_final_display);

    ImageView iv = new ImageView(this);
    iv.setBackgroundResource(R.drawable.images);
    layout.addView(iv);

    ImageView iv2 = new ImageView(this);
    iv.setBackgroundResource(R.drawable.images);
    layout.addView(iv2);

}

R.drawable.images is an image of a chicken I took from the Internet and placed in my drawable folder.

http://images3.wikia.nocookie.net/__cb20130606165308/animalcrossing/images/4/41/Chicken.jpg

My xml page looks like

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_final_display"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".FinalDisplay" >

However, only one chicken appears. Any help would be very much appreciated.

Upvotes: 2

Views: 241

Answers (2)

Randy
Randy

Reputation: 4381

@MH (in comments) is correct.

Replace the LinearLayout with RelativeLayout. The view which is on top is the view which was last added (or declared in XML).

This should work the way you want it to.

Java:

RelativeLayout layout = (RelativeLayout) findViewById(R.id.activity_final_display);

ImageView iv = new ImageView(this);
iv.setBackgroundResource(R.drawable.images);
layout.addView(iv);

ImageView iv2 = new ImageView(this);
iv.setBackgroundResource(R.drawable.images);
layout.addView(iv2);

XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_final_display"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".FinalDisplay" >

Upvotes: 1

SuNnY_sYeD
SuNnY_sYeD

Reputation: 513

ImageView iv = new ImageView(this);
iv.setBackgroundResource(R.drawable.images);
layout.addView(iv);

ImageView iv2 = new ImageView(this);
iv2.setBackgroundResource(R.drawable.images);
layout.addView(iv2);

Try that

Upvotes: 1

Related Questions