0

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)?

Compaq LE2202x
  • 2,030
  • 9
  • 45
  • 62
  • Well, if you are setting the parent's background, it will cover the parent. But, you'd like the brown to cover the green portion only, right? – Vikram Jul 20 '13 at 11:18
  • Post your layout xml file. – MaciejGórski Jul 20 '13 at 11:24
  • Yes. Actually this is a listview item layout and by the way, my parent layout is FrameLayout and one of the child is an ImageView, I wanted to change the ImageView's background specifically but I can't do this in the code because `view.setBackgroundDrawable(getResources().getDrawable(R.drawable.brown));` is inside `@Override public void onItemClick(AdapterView> parent, View view, int position, long id) {` listener. I didn't know how to get the ImageView inside the `view` object. But not knowing this, do you have an idea to resize brown to geen height dynamically? – Compaq LE2202x Jul 20 '13 at 11:31
  • What exactly do you mean by "I didn't know how to get the ImageView inside the view object". If what you want is to set the background to this `ImageView`, just use the `ImageView` variable and use the `setBackgroundDrawable()` method inside `onItemClick()`. You may have to declare the `ImageView` variable as `final`(by adding final in front of its declaration). – Vikram Jul 20 '13 at 11:38
  • can you put your layout xml? – Onur A. Jul 20 '13 at 11:51
  • I edited my question and posted my xml. @vikram I can't take the ImageView id or object since I can only take reference of the `view` – Compaq LE2202x Jul 20 '13 at 12:34
  • @CompaqLE2202x What is brown and what is green in your xml? – Vikram Jul 20 '13 at 12:44
  • Basically everything inside the FrameLayout is green and brown is the FrameLayout itself. – Compaq LE2202x Jul 20 '13 at 12:50
  • @CompaqLE2202x Try setting the attribute `layout_height` for `LinearLayout` to `match_parent`. – Vikram Jul 20 '13 at 13:54

0 Answers0