Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change android slidingdrawer handle button position

In default handle button in android SlidingDrawer in the centre of the drawer. Is it possible to change that position to left or right..? If it possible how can I do that, android:gravity="left" is not work in the button.

like image 813
Miuranga Avatar asked Aug 01 '11 04:08

Miuranga


2 Answers

Put your handle in a RelativeLayout, and set android:layout_alignParentRight="true" on the handle.

For example like this:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">

    <TextView android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

    <SlidingDrawer android:id="@+id/drawer"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:handle="@+id/handle" 
        android:content="@+id/content">

        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" 
            android:id="@+id/handle">
            <ImageView android:src="@drawable/icon"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true" />
        </RelativeLayout>

        <LinearLayout 
            android:gravity="center" 
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="#aaaa00" android:id="@+id/content">
            <ImageView android:src="@drawable/icon"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center">
            </ImageView>
        </LinearLayout>

    </SlidingDrawer>

</FrameLayout>
like image 51
HenrikS Avatar answered Oct 13 '22 00:10

HenrikS


The problem I had with HenrikS answer was that in my case I had some views behind the relative layout and when I tried to click them I realized that the relative layout was intercepting those clicks because the width was set to fill_parent.

To workaround this I modified the layout of the handler view (the ImageView) in my case to the left, hope it is helpful.

public class CustomSlidingDrawer extends SlidingDrawer {

public CustomSlidingDrawer(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

public CustomSlidingDrawer(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
}

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
    super.onLayout(changed, l, t, r, b);

    final int height = b - t;

    ImageView handle = (ImageView) getHandle();
    int childLeft = 0;
    int childWidth = handle.getWidth();
    int topOffset = 0;
    int bottomOffest = 0;
    int childHeight = handle.getHeight();

    int childTop = this.isOpened()?topOffset:height - childHeight + bottomOffest;

    handle.layout(childLeft, childTop , childLeft + childWidth, childTop+childHeight);
}
}
like image 42
garibay Avatar answered Oct 12 '22 22:10

garibay