Android how to set the layout the same size of an imageview programmatically

706 views Asked by At

I have two imageView size an relativelayout. Both images will be loaded programmatically and I want to set the size of the relativelayout equal to the size of imgBackground. How can I do that? I tried the following and it didn't work. The layout appears to be much larger than the imgBackground.

imageLayout.getLayoutParams().width = imgBackground.getWidth();
imageLayout.getLayoutParams().height = imgBackground.getHeight();

Here's the code for the layout

<RelativeLayout
     android:id="@+id/imageLayout"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:background="@color/black">
  <ImageView
       android:id="@+id/imgPhoto"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_alignParentLeft="true"
       android:layout_alignParentTop="true"
   />

  <ImageView
       android:id="@+id/imgBackground"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
   />
</RelativeLayout>
1

There are 1 answers

0
Ben On
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:id="@+id/imageLayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true">

        <ImageView
            android:id="@+id/imgBackground"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <ImageView
            android:id="@+id/imgPhoto"
            android:layout_alignBottom="@id/imgBackground"
            android:layout_alignLeft="@id/imgBackground"
            android:layout_alignRight="@id/imgBackground"
            android:layout_alignTop="@id/imgBackground"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:scaleType="center" />

    </RelativeLayout>
</RelativeLayout>