4

I've been trying to set an EditText box inside of a DrawerLayout, but reading carefully through the Android Training Website, they explain that the DrawerLayout is allowed to have only two child views. If I would like to do something like the next code, How should I approach it?

<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">
<!-- The main content view -->
<FrameLayout
    android:id="@+id/content_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
<!-- The navigation drawer -->
<EditText
    android:id="@+id/EditText01"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:hint="Search" >
</EditText>
<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="#111"/>
</android.support.v4.widget.DrawerLayout>
Luis Lavieri
  • 4,064
  • 6
  • 39
  • 69

1 Answers1

13

Do it like this:

<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">
<!-- The main content view -->
<FrameLayout
    android:id="@+id/content_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
<!-- The navigation drawer -->
<LinearLayout
    android:id="@+id/left_drawer"
    android:layout_height="wrap_content"
    android:layout_width="240dp"
    android:orientation="vertical"
    android:layout_gravity="start" >
    <EditText
        android:id="@+id/EditText01"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Search" >
    </EditText>
    <ListView android:id="@+id/left_drawer_list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp"
        android:background="#111"/>
</LinearLayout>

</android.support.v4.widget.DrawerLayout>

In other words, whatever the first view is, will be set as the content view, and whatever the second view is will be set in the drawer. So simply add elements inside the second view for drawer, and first for content. Cheers :)

LuckyMe
  • 3,820
  • 2
  • 27
  • 35
  • Thank you! Sounds logic, but how should I implement that new Layout into the Drawer List content? Should it be called in the adapter? right now, I am doing mDrawerList = (ListView) findViewById(R.id.left_drawer); and in the adapter I am calling the textview for the list. If I leave the code like I had it. The EditText box stays outside of the drawer. – Luis Lavieri Jul 05 '13 at 21:01
  • 1
    When I finally implemented it, it threw me an exception regarding that LinearLayout could not be a child of widget.DrawerLayout :( – Luis Lavieri Jul 05 '13 at 21:54
  • 1
    In your java code there is a mistake then, probably your close drawer function. Check this out http://stackoverflow.com/questions/16738595/is-it-possible-to-use-something-other-than-a-listview-as-sliding-drawer-in-drawe – LuckyMe Jul 05 '13 at 22:26
  • You would just have to split the action of the drawer from the actions of the ListView, give the LinearLayout an ID, and for every function that is dealing with the Drawer itself, give it that layout. And for the ListView functions give it the layout of the ListView, the ListView just becomes like any regular ListView you use. – LuckyMe Jul 06 '13 at 06:10
  • Thanks, but I don't understand. I can't convert a DrawerLayout to a LinearLayout. How can I set the drawer itself as a linear layout? I gave the linearlayout an id, and I have not touched the methods for the listview. – Luis Lavieri Jul 06 '13 at 18:19
  • 1
    Oh man, I feel stupid. I forgot to add android:layout_gravity="start" to the linearlayout :) it's working like a champ. Thanks again. I'll edit your answer to add that line. – Luis Lavieri Jul 06 '13 at 18:49
  • i tried this and it works but how can i apply the scrollview? I'm trying to add it above the LinearLayout – Kairi San Dec 02 '15 at 07:14
  • Your solution work indeed. But there is another problem. When I click edittext the navigation drawer close automatically. Do you have any idea to stop that from happening? – Iftieaq Apr 15 '18 at 07:51