Button on bottom of image with parameter adjustViewBounds Android studio

Viewed 103

I need a button on bottom of an image with the parameter android:adjustViewBounds="true".

This is what I'm looking for:

I need this

But this is what I'm getting:

But I have this

This is the code I'm using:

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

    <ImageView
        android:id="@+id/idImg"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true" //i really need this parameter because the height of each image is different
        android:src="@drawable/homero" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:text="See more"/>

</RelativeLayout>
1 Answers

If you want your ImageView and Button to be overlapping, they either need to reference each other, or have the same layout alignment.

In this example, aligning your Button to the bottom of your ImageView will resolve this.

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

    <ImageView
        android:id="@+id/idImg"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true" 
        android:src="@drawable/homero" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@id/idImg"
        android:text="See more"/>

</RelativeLayout>

The way you currently have it, your button is trying to get to the bottom of your RelativeLayout. Because your RelativeLayout has a height of wrap_content, it just keeps growing as the button tries to get to the bottom, eventually filling the screen!

Related