Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android DrawerLayout fails when using TOP and BOTTOM gravities

I'm just playing around with the new DrawerLayout recently included in support library. I've implemented just a simple to example with multiple drawers, while I was using START and END as gravities it all worked like a charm, but when trying to add a drawer with TOP or BOTTOM gravities it crashes.

Is it possible to use it for sliding drawers from top and/or bottom?

Below is the full code for my activity and the xml layout in a working state; If I try to change this:

<ListView android:id="@+id/right_drawer"
                  android:layout_width="240dp"
                  android:layout_height="match_parent"
                  android:layout_gravity="end"
                  android:choiceMode="singleChoice"
                  android:divider="@android:color/transparent"
                  android:dividerHeight="0dp"
                  android:background="#007799"/>

into this (notice changes in layout_width, layout_height and layout_gravity):

<ListView android:id="@+id/right_drawer"
                  android:layout_width="match_parent"
                  android:layout_height="300dp"
                  android:layout_gravity="bottom"
                  android:choiceMode="singleChoice"
                  android:divider="@android:color/transparent"
                  android:dividerHeight="0dp"
                  android:background="#007799"/>

and the line:

this.drawer.openDrawer(GravityCompat.START);

info this one:

this.drawer.openDrawer(Gravity.BOTTOM);

is when I get the following error:

05-16 05:22:33.981    1503-1503/es.luixal.test                 E/AndroidRuntime: FATAL EXCEPTION: main
        java.lang.RuntimeException: Unable to start activity ComponentInfo{es.luixal.test/es.luixal.test.MainActivity}: java.lang.IllegalArgumentException: View android.widget.RelativeLayout{42664c80 V.E..... ......I. 0,0-0,0} is not a sliding drawer
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
        at android.app.ActivityThread.access$600(ActivityThread.java:141)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:5039)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
        at dalvik.system.NativeStart.main(Native Method)
        Caused by: java.lang.IllegalArgumentException: View android.widget.RelativeLayout{42664c80 V.E..... ......I. 0,0-0,0} is not a sliding drawer
        at android.support.v4.widget.DrawerLayout.openDrawer(DrawerLayout.java:970)
        at android.support.v4.widget.DrawerLayout.openDrawer(DrawerLayout.java:1003)
        at es.luixal.test.MainActivity.onCreate(MainActivity.java:30)
        at android.app.Activity.performCreate(Activity.java:5104)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
        ... 11 more

Any clues of why can't I use this other gravities?

Thanks!

MainActivity.java

package es.luixal.test;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends Activity {

private DrawerLayout drawer;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    // filling list items:
    ListView listView = (ListView)findViewById(R.id.left_drawer);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, new String[]{
            "MenuItem 1",
            "MenuItem 2",
            "MenuItem 3"
    });
    listView.setAdapter(adapter);
    //
    this.drawer = (DrawerLayout)findViewById(R.id.drawer_layout);
    this.drawer.openDrawer(GravityCompat.START);
}

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    protected void onStart() {
        super.onStart();
    }
}

activity_main.xml

    <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

        <TextView android:id="@+id/text"
                  android:layout_width="match_parent"
                  android:layout_height="match_parent"
                  android:text="Hi!"
                />
    </RelativeLayout>

    <ListView 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/transparent"
              android:dividerHeight="0dp"
              android:background="#44aa00"/>

    <ListView android:id="@+id/right_drawer"
              android:layout_width="240dp"
              android:layout_height="match_parent"
              android:layout_gravity="end"
              android:choiceMode="singleChoice"
              android:divider="@android:color/transparent"
              android:dividerHeight="0dp"
              android:background="#007799"/>

</android.support.v4.widget.DrawerLayout>
like image 945
luixal Avatar asked May 16 '13 03:05

luixal


2 Answers

We have recently implemented this functionality in the Umano App and opened sourced it:

https://github.com/umano/AndroidSlidingUpPanel

like image 135
tokudu Avatar answered Oct 21 '22 11:10

tokudu


There is no official reference for DrawerLayout to use "Bottom" or "Top" Gravity - Source

public void openDrawer (int gravity)

Open the specified drawer by animating it out of view.

Parameters gravity

Gravity.LEFT to move the left drawer or Gravity.RIGHT for the right. GravityCompat.START or GravityCompat.END may also be used.

If you want to use other visual effects - use other library

like image 29
AndrewS Avatar answered Oct 21 '22 10:10

AndrewS