serverman
serverman

Reputation: 1314

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

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!

Upvotes: 6

Views: 17451

Answers (3)

serverman
serverman

Reputation: 1314

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>

Upvotes: 7

Orkun
Orkun

Reputation: 7268

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

Upvotes: 0

dcanh121
dcanh121

Reputation: 4695

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>

Upvotes: 0

Related Questions