placing an element to the right in a linear layout in android

16,017

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>
Share:
16,017
serverman
Author by

serverman

Updated on June 04, 2022

Comments

  • serverman
    serverman almost 2 years

    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
    serverman about 12 years
    Thanx for a super quick response - but sorry - that did not work.
  • Orkun Ozen
    Orkun Ozen about 12 years
    whatelse is in your linearlayout ?
  • serverman
    serverman about 12 years
    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...
  • Orkun Ozen
    Orkun Ozen about 12 years
    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 Ozen
    Orkun Ozen about 12 years
    Yeah it s definitely the layout orientation: android:orientation="vertical"
  • serverman
    serverman about 12 years
    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 !
  • Orkun Ozen
    Orkun Ozen about 12 years
    android:orientation="vertical" was missing all along.. :) remove it and u ll see..
  • serverman
    serverman about 12 years
    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!
  • Orkun Ozen
    Orkun Ozen about 12 years
    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 :)