Federico Navarrete
Federico Navarrete

Reputation: 3274

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.

Upvotes: 1

Views: 144

Answers (2)

Federico Navarrete
Federico Navarrete

Reputation: 3274

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!

Upvotes: 1

salmanseifian
salmanseifian

Reputation: 1082

You should add a LinearLayout as the child of DrawerLayout that has match_parent width and height. Then put your ListView at the bottom of that LinearLayout. Something like below. I don't know how exactly you want to your layout be, but below code put the listview at the bottom of LinearLayout

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.9"
        android:minHeight="25px"
        android:minWidth="25px"
        android:orientation="vertical">

        <android.webkit.WebView
            android:id="@+id/webView"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </LinearLayout>

    <ListView
        android:id="@+id/navList"
        android:layout_width="180dp"
        android:layout_height="0dp"
        android:layout_gravity="left|start"
        android:layout_weight="0.1"
        android:background="#ffffff"
        android:choiceMode="singleChoice" />

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

Upvotes: 0

Related Questions