ImageView won't fill parent

I have a ScrollView on one of my screens. I want the right edge to have a shadow. I decided the easiest way to do this was to make the child of the ScrollView a RelativeLayout, and have two children of the RelativeLayout -- one being a LinearLayout that will house the layout of the screen, and the second View being the shadow.

Like so...

<ScrollView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:scrollbars="none" >

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

                <LinearLayout
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical" >
                <!-- stuff -->
                </LinearLayout>

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="fill_parent"
                    android:src="@drawable/shadow"
                    android:layout_alignParentRight="true"
                    />
            </RelativeLayout>
        </ScrollView>

Unfortunately, this doesn't quite work. The ImageView is forcing its dimensions to be the size of the image file. It will not stretch vertically to be the height of the RelativeLayout. I've also tried "match_parent" to no avail. The image is a 9-patch.

Ideas?

Answers


Applying drawable content as the source of an ImageView somewhat carries with it an inherent requirement that you want the view to do what it can to accomodate the content without modifying the content itself very much. Typically, this is the behavior you would want out of an ImageView.

What you really want is the behavior you get by setting drawable content as the background of a view, for which you don't really need ImageView at all. A background is designed to simply stretch, fill, etc. to whatever size the view is. Also, since you are using RelativeLayout you can tell the view to match the bound of the view you are shadowing by adding an id and some extra layout_alignparameters.

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

            <LinearLayout
                android:id="@+id/content_layout"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical" >
            <!-- stuff -->
            </LinearLayout>

            <View
                android:layout_width="11dp"
                android:layout_height="fill_parent"
                android:layout_alignParentRight="true"
                android:layout_alignTop="@id/content_layout"
                android:layout_alignBottom="@id/content_layout"
                android:background="@drawable/shadow"
                />
        </RelativeLayout>

try this

<ImageView
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:background="@drawable/ic_launcher"
                android:scaleType="fitXY"
                android:layout_alignParentRight="true"
                />

here is what I get

and code id

<?xml version="1.0" encoding="utf-8"?>

<ScrollView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:fillViewport="true"
    android:scrollbars="none" >

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

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

            <!-- stuff -->
        </LinearLayout>

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_alignParentRight="true"
            android:background="@drawable/ic_launcher"
            android:scaleType="fitXY" />
    </RelativeLayout>
</ScrollView>


Your problem has nothing to do with the ImageView or 9-patch itself, but rather with the fact that you're wrapping everything in a ScrollView. A ScrollView will automatically force its children direct child to wrap its content, no matter whether you tell it to FILL_PARENT or MATCH_PARENT - both do exactly the same thing by the way; the only difference is the name, which reflects better the actual behaviour of the flag.

Fortunately ScrollView provides a way to force it to fill the viewport with a flag, which will make the behaviour pretty similar to setting FILL_PARENT to a regular view. Either add the attribute android:fillViewport or use setFillViewport() from code.

Edit: Just to be clear, you need to set that flag on the ScrollView. Also, if it's the ScrollView that should have the shadow, can you not send your 9-patch as background to it? I suppose it does depend on what your actual image looks like. Regarding you comment: yes, the RelativeLayout is flexible in terms of positioning and sizing children, but any child will still be bound to the size of its parent.

I do have the feeling that some of us may be working towards something different than what you have in mind. It would definitely help to clarify things with a simple drawing.


You wanted a Shadow towards the right of your image, Then use single layout with Horizontal Orientation, It's good that you have decide to use Relative Layout. Use

android:orientation="vertical"

inside this layout, add those two images. If you still have a doubt, give me those two images or sample images, i will give you the code


Need Your Help

Numbers | Email | Applescript

email applescript iwork

I would like to create a numbers spreadsheet. In that spreadsheet I want to have COL A Email Col B City Name COL C Other City Name and so on. I will manually enter that data in.

Generate pie chart with Gruff gem in Ruby

ruby charts gruff

I am attempting to create a pie chart using the gruff gem, but my chart is a black abyss regardless of what I do. This is my code: