6

I want to place an image to the right of the view. For that I am tyring to use something like

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent" >

...some other elements

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

    <ImageView
        android:id="@+id/imageIcon"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"            
        android:layout_gravity="right"
        android:src="@drawable/image_icon" >
    </ImageView>
</LinearLayout>

...
</RelativeLayout>

The above seems to put the image somewhere in the center. How do I ensure that the image is right aligned regardless of whether we are in portrait or landscape mode?

Thanx!

serverman
  • 1,314
  • 5
  • 22
  • 39

3 Answers3

7

Following seems to work. Two changes 1. use orientation = vertical as suggested by Zortkun 2. Use wrap_content in layout_width.

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/settingsIcon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:src="@drawable/settings_icon" >
    </ImageView>
</LinearLayout>
serverman
  • 1,314
  • 5
  • 22
  • 39
  • 1
    `android:orientation="vertical" ` was missing all along.. :) remove it and u ll see.. – Orkun Feb 15 '12 at 23:28
0

The default orientation of LinearLayout is horizontal. Make sure the Linear layout's orientation is set to Vertical ortherwise you cannot aling stuff horizontally.

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    >
Orkun
  • 6,998
  • 8
  • 56
  • 103
  • Thanx for a super quick response - but sorry - that did not work. – serverman Feb 15 '12 at 23:00
  • whatelse is in your `linearlayout` ? – Orkun Feb 15 '12 at 23:09
  • Nothing else. The remaining stuff is within the outer relative layout - basically I want a row with only one image at the right hand side. I can of course use left margin (that works) but I want to use alignment so I do not have to worry about portrait/landscape mode... – serverman Feb 15 '12 at 23:10
  • yeah you shouldnT use margins in that situation. It should be easy tho. Could you please post your entire xml so we can test better? – Orkun Feb 15 '12 at 23:14
  • Yeah it s definitely the layout orientation: `android:orientation="vertical"` – Orkun Feb 15 '12 at 23:21
  • 1
    Hi Zortkun - thanx for your answer. I did not need to have the "center}right". I thought I had tried this before. Perhaps the "orientation = vertical" is the new thing. Thank you ! – serverman Feb 15 '12 at 23:27
  • Hi ZortKun, Can you please edit your answer to use the layout I gave in my answer (since I know that works in my particular scenario). I would like to mark your answer as the right one. Thanx! – serverman Feb 16 '12 at 15:42
  • damn you are right! i lost the fight :D i missed that one.. sorry. well, you can always mark urs as the answer. doNT worry :) – Orkun Feb 16 '12 at 16:28
0

You don't need a Linearlayout if you use only one view inside the Linearlayout.

Anyway its here,

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent" >

//...some other elements

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

    <ImageView
        android:id="@+id/imageIcon"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"            
        android:layout_gravity="right"
        android:src="@drawable/image_icon" >
    </ImageView>
</LinearLayout>

</RelativeLayout>
dcanh121
  • 4,665
  • 11
  • 37
  • 84