I'm trying to implement a SlidingDrawer
that will occupy the full screen width, but whose height is determined dynamically by its contents: in other words, standard fill_parent
layout behaviour for the width and wrap_content
for the height. That's exactly how I've specified it in the layout XML (see below) but the sliding drawer always opens to the full screen height. The height of my content varies, but typically it's only about half the screen height, so I end up with a big gap underneath it. What I'd like is for the content to sit neatly on the bottom of the screen.
I've tried everything I can think of to fix it but nothing's worked so far. If I set the SlidingDrawer
's layout_height
to a specific value (e.g. 160dip
) it works, but that's not what I need: it has to be dynamic. Of course I've made sure all the child elements have their height set to wrap_content
too.
The documentation on SlidingDrawer is a bit vague on this and I haven't been able to find any examples that do what I'm after either. If anyone can see where I'm going wrong I'd really appreciate your help!
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ViewFlipper
android:id="@+id/ImageFlipper"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="@+id/imageView0"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="centerCrop" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="centerCrop" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="centerCrop" />
</ViewFlipper>
<SlidingDrawer
android:id="@+id/infoDrawer"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:handle="@+id/infoDrawerHandle"
android:content="@+id/infoDrawerContent"
android:allowSingleTap="false"
android:layout_alignParentBottom="true"
android:orientation="vertical" >
<!-- Sliding drawer handle -->
<ImageView
android:id="@id/infoDrawerHandle"
android:src="@drawable/info_handle_closed"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<!-- Sliding drawer content: a scroller containing a group of text views
laid out in a LinearLayout -->
<ScrollView
android:id="@id/infoDrawerContent"
android:background="@drawable/info_background"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fillViewport="false" >
<LinearLayout
android:id="@id/infoDrawerContent"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingRight="5dip" >
<TextView
android:id="@+id/infoTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ffffff"
android:textSize="16dip"
android:textStyle="bold" />
<TextView
android:id="@+id/infoCreator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ffffff"
android:textSize="14dip"
android:textStyle="italic"
android:paddingBottom="10dip" />
<TextView
android:id="@+id/infoDescription"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ffffff"
android:textSize="14dip"
android:paddingBottom="10dip" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ffcc00"
android:textSize="14dip"
android:textStyle="bold"
android:text="@string/heading_pro_tip" />
<TextView
android:id="@+id/infoProTip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ffcc00"
android:textSize="14dip" />
</LinearLayout>
</ScrollView>
</SlidingDrawer>
</RelativeLayout>
The
onMeasure()
method of the SlidingDrawer class basically overrides the layout modes tofill_parent
, this is whylayout_height="wrap_content"
is not working.To get around this, you can extend SlidingDrawer with a re-implemented
onMeasure()
method that honors thelayout_width
andlayout_height
attributes. You can then use this custom class in your XML layout by replacing<SlidingDrawer ...>
with<fully.qualified.package.ClassName ...>
.Note that since the drawer will no longer be filling the parent layout, you will have to enclose it in a LinearLayout with the gravity attribute set to the edge where the drawer should be.
Below are a class I have created for this purpose and an example layout.
WrappingSlidingDrawer class :
Example layout (assuming WrappingSlidingDrawer is in package com.package) :
unfortunately you can't set the height, but rather the opposite of that. the topOffset attribute will determine how tall to make the sliding drawer, but its what to shave off rather than how tall it will be.
just set to pmargin in sliding drawer in your xml
It works for me:
XML Layout:
it is better to read the parameter without hardcoding the string:
Another issus is in the onMeasure.
I used the following code:
seydhe's answer has a small issue.
The first argument to the getAttributeIntValue needs to be the full namespace, not just "android". So the code snippet should be:
I was having trouble getting this to work with a horizontal sliding drawer until I realized that it was not finding the orientation attribute and was therefore treating it as vertical.