views:

102

answers:

2

I'd like to know how to draw two PNG pictures onto the screen.

My XML layout: (named paperxml.xml)

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

    <ImageView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/paperid"
        android:src="@drawable/paperrepresentation"
    />

    <ImageView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/rockid"
        android:src="@drawable/rockrepresentation"
        android:layout_alignTop="@id/paperid"
    />

</RelativeLayout>

What would be the java code to instantiate the XML layout and display both ImageViews on the screen at the same time? Simply calling setContentView(R.drawable.paperxml); crashes my application on startup.

+2  A: 

Replace the xml with:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layoutid"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"  >

    <ImageView android:id="@+id/paperid"
        android:src="@drawable/paperrepresentation"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <ImageView android:id="@+id/rockid"
        android:src="@drawable/rockrepresentation"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</LinearLayout>

Explanation:

  • RelativeLayout doesn't use android:orientation="vertical".
  • Every view should have android:layout_width and android:layout_height.
  • Add the xmlns:android thing just in the first element.
Macarse
I put the XML in but it only displays one ImageViewHere's a screenshot of the emulator I took.http://i852.photobucket.com/albums/ab87/thomasjakway1/Capture.pngIts worth mentioning that the file shown is paperrepresentation
dragonwrenn
That's because of the size. Try playing with the `layout_width` and `layout_height` use something like "40dp".Also remember that the LinearLayout is set to Vertical.
Macarse
+1  A: 

Calling setContentView(R.drawable.paperxml); is not crashing your code - it's your XML file. Macarse has the correct answer to your problem, and keep your code the same!

You may also want to look at the View Tutorials for some examples of setting up your XML and using different View objects.

Aurora