0

today i'm working on layout and i have a question , basic i think.

I have a linear layout and an imageview on it. I want to display the image and the left or on the right of the layout

I tried , layout_gravity It's important to precise that i want to do it on a linear and not on a relative or other stuff.

Thanks by advance , a screen to precise my question

http://hpics.li/fc67805

my linear with the image

    <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:orientation="horizontal" >

            <ImageView
                android:id="@+id/Reboot"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="0.3"
                android:layout_gravity="left"
                android:src="@drawable/arreter" />
</LinearLayout>
JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
user3668100
  • 103
  • 1
  • 1
  • 9

4 Answers4

1

What's happening is that your ImageView is "matching" the size of the parent LinearLayout, so there's no way to align it to either the left or the right...it's pointless in this case because the view takes the whole space anyway. If you really want to visualize the effect of the image being aligned to either the left or the right of its parent, you can change the layout_width and layout_height of the ImageView to wrap_content as below...

     <ImageView
            android:id="@+id/Reboot"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="left"
            android:src="@drawable/arreter" />

also, notice that I removed the layout_weight attribute since it is also useless for a single child view inside a LinearLayout

Leo
  • 14,625
  • 2
  • 37
  • 55
0

Use this layout to align the ImageView to either right/left

<?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="wrap_content"
            android:layout_weight="1"
            android:orientation="vertical" >

            <ImageView
                android:id="@+id/Reboot"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"

                android:layout_gravity="left"         // you can use right also. It works.
                android:src="@drawable/ic_launcher" />
</LinearLayout>

Your LinearLayout orientation is horizontal, so you can place the child views in top and bottom using android:layout_gravity. For horizontal LinearLayout right and left doesn't work, they work only for vertical LinearLayout. Hope this information is useful :)

Aniruddha
  • 4,477
  • 2
  • 21
  • 39
0

Try this way,hope this will help you to solve your problem.

To Left Side

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <ImageView
            android:id="@+id/Reboot"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:adjustViewBounds="true"
            android:src="@drawable/ic_launcher" />
</LinearLayout>

To Right Side

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="right">

        <ImageView
            android:id="@+id/Reboot"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:adjustViewBounds="true"
            android:src="@drawable/ic_launcher" />
</LinearLayout>
Haresh Chhelana
  • 24,720
  • 5
  • 57
  • 67
0

This question has probably already been answered but in case someone else finds this

Layout_gravity and Gravity are different. Layout_gravity changes the position of the item within its ViewGroup while andorid:gravity changes the position of the item's contents.

There's a good discussion about the difference here

Now, since both the LinearLayout's width and the ImageView's width are match_parent, layout_gravity won't work because they're already the same size. You need to keep the LinearLayout's width at match_parent and change the ImageView's width to wrap_content. Then, you can use android:layout_gravity="right" or android:layout_gravity="left" to move the image right or left.

NOTE: This also applies to moving an image up/down. Just change the ViewGroup's and ImageView's height instead of the width and use android:layout_gravity="top" or android:layout_gravity="bottom"

App Coder
  • 71
  • 1
  • 4