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>
Author by
serverman
Updated on June 04, 2022Comments
-
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 about 12 yearsThanx for a super quick response - but sorry - that did not work.
-
Orkun Ozen about 12 yearswhatelse is in your
linearlayout
? -
serverman about 12 yearsNothing 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 about 12 yearsyeah 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 about 12 yearsYeah it s definitely the layout orientation:
android:orientation="vertical"
-
serverman about 12 yearsHi 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 about 12 years
android:orientation="vertical"
was missing all along.. :) remove it and u ll see.. -
serverman about 12 yearsHi 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 about 12 yearsdamn you are right! i lost the fight :D i missed that one.. sorry. well, you can always mark urs as the answer. doNT worry :)