Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 660
serverman Avatar asked Feb 15 '12 22:02

serverman


People also ask

How do you set a linear layout on the right side?

To create a linear layout in which each child uses the same amount of space on the screen, set the android:layout_height of each view to "0dp" (for a vertical layout) or the android:layout_width of each view to "0dp" (for a horizontal layout). Then set the android:layout_weight of each view to "1" .

How do I change the position of a button in linear layout?

You can set the android:layout_weight='1' and both buttons will share the screen equally(side by side) or if you want the extra space between them, you can place a button each in a linear layout and set the android:layout_gravity to left and right for each. Save this answer.

How do you center align an item in linear layout?

To center align LinearLayout, assign android:gravity attribute of this LinearLayout with the value “center”.

Can linear layout be placed in relative layout?

We can use LinearLayout inside RelativeLayout.


1 Answers

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>
like image 151
serverman Avatar answered Nov 14 '22 22:11

serverman