Walter
Walter

Reputation: 189

How to move the ImageView extreme right in android LinearLayout?

This is my xml. I need to place my icon to extreme right. But it's not moving to extreme right. "tri_logo" I have given gravity as right. But still not moving to right.

 <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/toplayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:clickable="true"
        android:orientation="vertical"    >

        <LinearLayout
            android:id="@+id/lout_profile_header"
            android:layout_width="match_parent"
            android:layout_height="@dimen/top_view_height"
            android:clickable="true"
            android:gravity="center"
            android:orientation="horizontal">

            <com.test.customviews.CircleImageView
                android:id="@+id/imgTopProfile"
                android:layout_width="@dimen/profilie_icon_size"
                android:layout_height="@dimen/profilie_icon_size"
                android:layout_margin="@dimen/hdr_img_padding"
                android:src="@mipmap/ic_profile" />

            <TextView
                android:id="@+id/textTopProfile"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:layout_marginLeft="@dimen/hdr_name_padding" />
            <ImageView
                android:id="@+id/tri_logo"
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:layout_gravity="right"
                android:src="@drawable/tri_logo"/>
        </LinearLayout>
    </LinearLayout>

ImageView tri_logo is not moving to right.

Upvotes: 0

Views: 2321

Answers (5)

Navneet Krishna
Navneet Krishna

Reputation: 5017

Try this

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toplayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:orientation="vertical">

<LinearLayout
    android:id="@+id/lout_profile_header"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:clickable="true"
    android:weightSum="3"
    android:layout_gravity="right"
    android:orientation="horizontal">

    <com.test.customviews.CircleImageView
        android:id="@+id/imgTopProfile"
        android:layout_weight="1"
        android:layout_width="@dimen/profilie_icon_size"
        android:layout_height="@dimen/profilie_icon_size"
        android:layout_margin="@dimen/hdr_img_padding"
        android:src="@mipmap/ic_profile" />

    <TextView
        android:layout_weight="1"
        android:id="@+id/textTopProfile"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
         />
    <ImageView
        android:layout_weight="1"
        android:id="@+id/tri_logo"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_gravity="right"
        android:src="@drawable/voyage"/>
</LinearLayout>
</LinearLayout>

You could use RelativeLayout and then use layout_alignParentEnd=true, if you want to stick on to LinearLayout try above one

Upvotes: 1

SpiritCrusher
SpiritCrusher

Reputation: 21043

Use RelatiVeLayout as immediate parent .

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/toplayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:clickable="true"
    android:orientation="vertical"    >

    <RelativeLayout
        android:id="@+id/lout_profile_header"
        android:layout_width="match_parent"
        android:layout_height="@dimen/top_view_height"
        android:clickable="true"
        >

        <com.test.customviews.CircleImageView
            android:id="@+id/imgTopProfile"
            android:layout_width="@dimen/profilie_icon_size"
            android:layout_height="@dimen/profilie_icon_size"
            android:layout_centerVertical="true"
            android:layout_margin="@dimen/hdr_img_padding"
            android:src="@mipmap/ic_profile" />

        <TextView
            android:id="@+id/textTopProfile"
            android:layout_width="match_parent"
            android:layout_centerVertical="true"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:layout_toLeftOf="@+id/tri_logo"
            android:layout_toRightOf="@+id/imgTopProfile"
            android:layout_marginLeft="@dimen/hdr_name_padding" />
        <ImageView
            android:id="@+id/tri_logo"
            android:layout_width="30dp"
            android:layout_centerVertical="true"
            android:layout_height="30dp"
            android:layout_alignParentRight="true"
            android:src="@drawable/tri_logo"/>
    </RelativeLayout>
</LinearLayout>

Upvotes: 0

Santanu Sur
Santanu Sur

Reputation: 11477

Wrap up your ImageView in a RelativeLayout with match_parent width and use android:layout_alignParentRight="true" like this:

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">  

    <ImageView
        android:id="@+id/tri_logo"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_alignParentRight="true"
        android:src="@drawable/tri_logo"/>
</RelativeLayout>

Upvotes: 1

Shanto George
Shanto George

Reputation: 994

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/toplayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:clickable="true"
    android:orientation="vertical"    >

    <RelativeLayout
        android:id="@+id/lout_profile_header"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:clickable="true"
        android:gravity="center">

        <com.test.customviews.CircleImageView
            android:id="@+id/imgTopProfile"
            android:layout_width="@dimen/profilie_icon_size"
            android:layout_height="@dimen/profilie_icon_size"
            android:layout_margin="@dimen/hdr_img_padding"
            android:src="@mipmap/ic_profile" />

        <TextView
            android:id="@+id/textTopProfile"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_marginLeft="100dp" />
        <ImageView
            android:layout_alignParentRight="true"
            android:id="@+id/tri_logo"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_gravity="right"
            android:src="@drawable/tri_logo"/>
    </RelativeLayout>
</LinearLayout>

Upvotes: 0

MarGin
MarGin

Reputation: 2518

Use the tools a LinearLayout provides. Give weight to your child views and change the parent layout's gravity to right

Upvotes: 0

Related Questions