How to resize image height based on the parent layout content height dynamically?

318 views Asked by At

I have a parent layout with android:layout_height="wrap_content" and in my class code, it should set the layout background from a specific image, but the image height is bigger than the content height. I wanted to resize the image height of the background same to the height of the content of the parent layout. How can I do this?

enter image description here

Black - Parent Layout

Green - Layout contents

Brown - Layout background (from drawable)

Here's how I set my parent layout background: view.setBackgroundDrawable(getResources().getDrawable(R.drawable.brown));

EDIT:

Here's my xml:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >

<ImageView
    android:id="@+id/item_bg"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:adjustViewBounds="true"
    android:src="@android:color/transparent"
    android:translationX="-10dp" />

<LinearLayout
    android:id="@+id/item_song"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#b97a57"
    android:orientation="horizontal"
    android:padding="5dip" >

    <!-- ListRow Left sied Thumbnail image -->

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="5dip"
        android:layout_weight="0"
        android:background="@color/aqua"
        android:padding="1dip" >

        <ImageView
            android:layout_width="50dip"
            android:layout_height="50dip"
            android:src="@color/gray" />

        <!-- Thumbnail Progressbar -->

        <ProgressBar
            android:id="@+id/progressbar"
            style="?android:attr/progressBarStyleSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true" />
    </RelativeLayout>
</LinearLayout></FrameLayout>

and my where I call this layout in my java class listener:

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    view.setBackgroundDrawable(getResources().getDrawable(R.drawable.list_item_selected));
     }

This is where I meant I can't get reference of the ImageView itself because I can only have the View view from onItemClick. This is a ListView and I wanted to change the background when clicked.

In any way, how could I resize the height of the content so the background(brown) height would just fill the parent layout(green)?

0

There are 0 answers