Search code examples
androidlistviewdrawerlayoutnavigation-drawer

How to set a ListView at the bottom of a NavigationDrawer?


I'm developing an app and I have an issue that I cannot a way of displaying the ListView at the bottom of the DrawerLayout, this is my code:

<?xml version="1.0" encoding="utf-8"?>
<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">
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:minWidth="25px"
        android:minHeight="25px">
        <android.webkit.WebView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/webView" />
    </LinearLayout>
    <ListView
        android:id="@+id/navList"
        android:layout_width="180dp"
        android:layout_height="match_parent"
        android:choiceMode="singleChoice"
        android:layout_gravity="left|start"
        android:background="#ffffff" />
</android.support.v4.widget.DrawerLayout>

This is how currently looks like:

example

I tried setting the ListView inside of a RelativeLayout and it didn't work at all (I used for example one of the current answers although I tried by myself before.). This is what happens:

image2

Also, I tried to set an ImageView too just to create some space, but in both cases the code broke the code.

This is what I'm trying to do:

example

Does anyone have any idea? Thanks.


Solution

  • There are several changes in order to make it work:

    First to create a Fragment class:

    public class BlankFragment : Fragment
    {
        public BlankFragment()
        {
        }
    
        public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
            // Inflate the layout for this fragment
            return inflater.Inflate(Resource.Layout.BlankFragment, container, false);
        }
    }
    

    Second, replace the ListView with a Fragment in the XML:

    <fragment
        android:name="your package name.BlankFragment"
        android:layout_width="180dp"
        android:layout_height="match_parent"
        android:layout_gravity="left"/>
    

    And finally:

    Move the ListView to a Fragment XML:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="180dp"
        android:layout_height="match_parent"
        android:background="#e00808"
        tools:context=".BlankFragment">
    
        <!-- TODO: Update blank fragment layout -->
    
        <ListView
            android:id="@+id/navList"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:choiceMode="singleChoice"
            android:layout_alignParentBottom="true"
            android:background="#ffffff" />
    
    </RelativeLayout>
    

    Also, you need to change the android:layout_height="match_parent" by android:layout_height="wrap_content". Then the result is going to be below as expected.

    img

    I got a lot of support from robbit in the Xamarin Forum:

    https://forums.xamarin.com/discussion/129313/how-to-set-a-listview-at-the-bottom-of-a-navigationdrawer

    You can check his answers! Thanks robbit!