Search code examples
androidandroid-imageviewandroid-relativelayoutandroid-gravity

How to attach ImageView to right border of RelativeLayout


I want to attach ImageViews to each other. I tried to set gravity and alignParentRight/alignParentLeft but can`t achieve my goal. For simplicity I have set color backgrounds for ViewGroups. Do you have any ideas how to make it done?

enter image description here

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="@dimen/share_dope_max_image_height"
        android:layout_marginTop="@dimen/share_dope_margin_3"
        android:background="#0DD"
        android:orientation="horizontal"
        android:paddingLeft="@dimen/share_dope_images_container_horizontal_padding"
        android:paddingRight="@dimen/share_dope_images_container_horizontal_padding">

        <RelativeLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="0.5"
            android:background="#D0D"
            android:gravity="right"
            >
                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:layout_alignParentRight="true"
                    android:src="@drawable/girl3" />

        </RelativeLayout>

        <RelativeLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="0.5"
            android:background="#DD0"
            android:gravity="left"
            >

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:layout_alignParentLeft="true"
                    android:src="@drawable/girl4" />

        </RelativeLayout>

    </LinearLayout>

Solution

  • Found the exact solution for this problem here: http://www.ryadel.com/en/android-proportionally-stretch-imageview-fit-whole-screen-width-maintaining-aspect-ratio/