John Doe
John Doe

Reputation: 9764

Displaying imageView near the LinearLayout

I want to display a picture near a LinearLayout. I want to make the following:

+---------------------------------------------------+
|                                                   |
|textView1       +------------+                     |
|button1         |imageView1  |                     |
|                |            |                     |
|                |            |                     |
|                |            |                     |
|                +------------+                     |
|                                                   |
|                                                   |
|                                                   |
+---------------------------------------------------+

But with this code:

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

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="96dp"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Medium Text"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button" />

    </LinearLayout>

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

</LinearLayout>

I probably get something like that (I don't see the picture):

+---------------------------------------------------+
|                                                   |
|textView1                                          |
|button1                                            |
|                                                   |
|                                                   |
|                                                   |
|                                                   |
|                                                   |
|                                                   |
|                                                   |
+---------------------------------------------------+
 imageView1

How can I solve that? Thanks in advance.

Upvotes: 0

Views: 188

Answers (4)

Arif Nadeem
Arif Nadeem

Reputation: 8604

You need to include your ImageView within the Layout to achieve what you want..

Try this XML file instead

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

<LinearLayout
    android:id="@+id/linearLayout1"
    android:layout_width="96dp"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Medium Text"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_gravity = "center"
        android:layout_marginBottom = "40dp"
        android:layout_height="wrap_content"
        android:src="@drawable/picture" />

</LinearLayout>

</LinearLayout>

So here you tell Android that you want the ImageView to be a part of LinearLayout, ofcourse you can do better if you use a RelativeLayout, but this was a solution to your problem.

EDIT:

If you want to use Relative Layout to achieve the same then here is the way..

<?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" >

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_marginTop="40dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Medium Text"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button" />
    </LinearLayout>

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="50dp"
        android:layout_centerHorizontal="true"
        android:src="@drawable/ic_launcher" />

</RelativeLayout>

Upvotes: 0

Ashwin N Bhanushali
Ashwin N Bhanushali

Reputation: 3882

Use the following code

<LinearLayout
    android:id="@+id/linearLayout1"
    android:layout_width="96dp"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Medium Text"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

</LinearLayout>

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/picture" 
    android:totheRightof=android:id="@+id/linearLayout1"
    android:marginLeft="20dp"/>

Upvotes: 0

waqaslam
waqaslam

Reputation: 68187

LinearLayout put views either in vertical or horizontal order. You need to use RelativeLayout to achieve this.

Set android:layout_centerInParent="true" to your ImageView when using RelativeLayout and it will do the magic

Upvotes: 1

goodm
goodm

Reputation: 7295

You need to use the RelativeLayout to manage this.

<?xml version="1.0" encoding="utf-8"?>

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Medium Text"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />


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

Now everything is on the top screen. Try to setup everything as you like.

Upvotes: 2

Related Questions