8

How to slide ExpandableListView at DrawerLayout form right to left

i want to show ExpandableListView at right corner of screen , how can i do that ??

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <ExpandableListView
        android:id="@+id/left_drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:divider="@android:color/white"
        android:dividerHeight="0dp"
      />
</android.support.v4.widget.DrawerLayout>
user1571714
  • 305
  • 2
  • 5
  • 12

3 Answers3

23

Change android:layout_gravity="start" to either android:layout_gravity="end" or android:layout_gravity="right" (using end will put the drawer on the left side of the screen for right-to-left configurations)

EDIT

It appears that the ActionBarDrawerToggle is looking for a drawer on the same side as the action bar Home icon (Gravity.START), which would be the left side for left-to-right layout directions. If you will always have the drawer on the right, you could do something like this:

mDrawerToggle = new ActionBarDrawerToggle(this, mDrawer, R.drawable.ic_drawer, R.string.open_drawer, R.string.close_drawer) {

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item != null && item.getItemId() == android.R.id.home) {
            if (mDrawer.isDrawerOpen(Gravity.RIGHT)) {
                mDrawer.closeDrawer(Gravity.RIGHT);
            } else {
                mDrawer.openDrawer(Gravity.RIGHT);
            }
        }
        return false;
    }
};

If the drawer will always be opposite the action bar Home icon, you could use Gravity.END instead.

Karakuri
  • 38,365
  • 12
  • 84
  • 104
  • when i do that application crash – user1571714 Jul 22 '13 at 16:05
  • I am able to use `android:layout_gravity="end"` just fine with one of my little samples, so something else must be causing your crash. – Karakuri Jul 22 '13 at 16:14
  • 2
    07-23 14:40:18.200: E/AndroidRuntime(20593): FATAL EXCEPTION: main 07-23 14:40:18.200: E/AndroidRuntime(20593): java.lang.IllegalArgumentException: No drawer view found with absolute gravity LEFT 07-23 14:40:18.200: E/AndroidRuntime(20593): at android.support.v4.widget.DrawerLayout.openDrawer(DrawerLayout.java:1000) 07-23 14:40:18.200: E/AndroidRuntime(20593): at android.support.v4.app.ActionBarDrawerToggle.onOptionsItemSelected(ActionBarDrawerToggle.java:248) – user1571714 Jul 23 '13 at 12:43
  • 07-23 14:40:18.200: E/AndroidRuntime(20593): at com.example.android.navigationdrawerexample.MainActivity.onOptionsItemSelected(MainActivity.java:141) 07-23 14:40:18.200: E/AndroidRuntime(20593): at android.app.Activity.onMenuItemSelected(Activity.java:2502) 07-23 14:40:18.200: E/AndroidRuntime(20593): at com.android.internal.widget.ActionBarView$3.onClick(ActionBarView.java:170) 07-23 14:40:18.200: E/AndroidRuntime(20593): at android.view.View.performClick(View.java:3591) 07-23 14:40:18.200: E/AndroidRuntime(20593): at android.view.View$PerformClick.run(View.java:14263) – user1571714 Jul 23 '13 at 12:45
  • 07-23 14:40:18.200: E/AndroidRuntime(20593): at android.os.Handler.handleCallback(Handler.java:605) 07-23 14:40:18.200: E/AndroidRuntime(20593): at android.os.Handler.dispatchMessage(Handler.java:92) 07-23 14:40:18.200: E/AndroidRuntime(20593): at android.os.Looper.loop(Looper.java:137) 07-23 14:40:18.200: E/AndroidRuntime(20593): at android.app.ActivityThread.main(ActivityThread.java:4507) 07-23 14:40:18.200: E/AndroidRuntime(20593): at java.lang.reflect.Method.invokeNative(Native Method) 07-23 14:40:18.200: E/AndroidRuntime(20593): at java.lang.reflect.Method.invoke(Method.java:511) – user1571714 Jul 23 '13 at 12:46
  • 07-23 14:40:18.200: E/AndroidRuntime(20593): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790) 07-23 14:40:18.200: E/AndroidRuntime(20593): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557) 07-23 14:40:18.200: E/AndroidRuntime(20593): at dalvik.system.NativeStart.main(Native Method) – user1571714 Jul 23 '13 at 12:47
  • I've edited my post to address your crash. (Next time please update your POST instead of putting this in the comments...) – Karakuri Jul 23 '13 at 15:29
  • thank you , It has become open from the right side but drawer at the left side , Whether Gravity.END or Gravity.RIGHT , – user1571714 Jul 24 '13 at 11:53
5

I have the same problem since my app is Arabic and must begin from the right.

After testing all solutions here, I found that the best is:

  • add this line in manifest : android:supportsRtl="true"
  • in your activity add this code : getWindow().getDecorView().setLayoutDirection(View.LAYOUT_DIRECTION_RTL);
  • in your listview don't use : android:layout_gravity="right" because if you use right you get the error in more Android versions like 4.0.4

The solution is the use of: android:layout_gravity="start", because in this case the listview follows the gravity of the page.

ebo
  • 2,717
  • 1
  • 27
  • 22
yacine
  • 51
  • 1
  • 1
2

It is a bug in the home button click in ActionBar that shows itself in line:

if (mDrawerToggle.onOptionsItemSelected(item)) {
    return true;
}

remove the lines I mentioned, where you have method:

@Override
public boolean onOptionsItemSelected(MenuItem item)
{
   if (mDrawerToggle.onOptionsItemSelected(item)) {
      return true;
   }
   //....
}

and replace it with:

@Override
public boolean onOptionsItemSelected(MenuItem item)
{
    if (item != null && item.getItemId() == android.R.id.home) {
        if (mDrawerLayout.isDrawerOpen(Gravity.RIGHT)) {
            mDrawerLayout.closeDrawer(Gravity.RIGHT);
        } else {
            mDrawerLayout.openDrawer(Gravity.RIGHT);
        }
        return true;
    }
    //...
}

Also do not forget to change the gravity of ListView to right: android:layout_gravity="right"

Bob
  • 22,810
  • 38
  • 143
  • 225