How to change android slidingdrawer handle button position
Asked Answered
A

7

13

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.

Affront answered 1/8, 2011 at 4:9 Comment(1)
Looking for a solution to this question if anyone has any suggestions on how to do this or how to modify the SlidingDrawer to do this...Cortisone
H
21

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>
Howlan answered 2/8, 2011 at 5:55 Comment(4)
This answer should be accepted--HenrikS' solution worked perfectly for me.Overstay
Doesn't work for me. I can specify an 'android:layout_alignParentRight' attribute in the 'handle' View (or a child of the 'handle' View, as above) such that it compiles but unfortunately it's ignored when run (so that the ImageView is still centre-aligned). To add: with width of the RelativeLayout specified as "fill_parent", even if the code did run, the whole width of the screen would act as the handle which I am guessing is not what is desired, not for me at least.Cortisone
It did not work for some people, because they missed the fact that the SlidingDrawer's handle should now be the relative layout inside which is the button. Not the button id itself. This is really weird (and I couldn't have figured out by myself...) but Android layouts are more Harry Potter magic than programming...Arnica
Android layouts is programming not Harry Potter magic. RespectDiscuss
P
5

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);
}
}
Pounce answered 25/6, 2012 at 19:38 Comment(1)
what changes should i make if i want it on the right side, i am not able to figure out, please helpGooch
S
1

The idea is simple: set the handle's padding to the correct value depending on the alignment you want.

In the example below, I derive a class from SlidingDrawer and override onLayout as follows. My sliding drawer is at the bottom of the screen, and I want the handle to be left-aligned rather than center-aligned.

public class ToolDrawer extends SlidingDrawer {
    private int m_handleWidth = 0;  // original handle width

    public boolean onTouchEvent(MotionEvent event) {
        float x = event.getX();
        if (x <= m_handleWidth) {
            return super.onTouchEvent(event);
        }
        return false;
    }

    protected void onLayout (boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);

        if (changed) {
            ImageView iv = (ImageView) getHandle();
            iv.setPadding(0, 0, getWidth() - iv.getWidth(), 0);
            if (m_handleWidth == 0) {
                m_handleWidth = iv.getWidth();
            }
        }
    }
}

It's as simple as that.

Sibilate answered 19/5, 2012 at 18:25 Comment(1)
Hey , ive my slider on left side of the screen and i want the sliding menu to come from top to bottom , rather then from left to right ; could you help me , as to what has to be done in the XML file . I appreciate your rply deeply .Haviland
S
0

I tried android:paddingBottom="300dp" for the ImageView and it works fine, also with left, right and top. But you have to set the padding from the middle-position, so bottom="300dp" means the handle move up 300 from the middle

Skirmish answered 9/1, 2012 at 14:50 Comment(0)
C
0

A simpler solution for left-aligned handle:

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
    super.onLayout(changed, l, t, r, b);
    final View handle = getHandle();
    handle.layout(0, handle.getTop(), handle.getWidth(), handle.getBottom());
}
Chamorro answered 19/2, 2013 at 11:23 Comment(0)
H
0

I have researched this problem for 2 days and I realized that you need to use another library. Because, when you use RelativeLayout or LinearLayout, you're just get fill_parent screen and of course it will be affected by touching ( although you don't touch on icon ).

So, I use SlidingTray from Sileria library ( ver 3.5 ) http://aniqroid.sileria.com/doc/api/

Then like this

Handsaw answered 26/1, 2016 at 19:46 Comment(0)
S
-1

I found the solution for garibay You just need to set android:clickable="true" on content layout of sliding drawer.

Slumgullion answered 21/7, 2014 at 14:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.