venugopal
venugopal

Reputation: 273

android two listview one below the other

I have the two listviews listview2 and listview3

what i want is listview3 should be displayed below listview2 but it gets overlapped

How can i get the correct output i.e to be displayed one below the other

and last ListView is navigation drawer

<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">


<RelativeLayout 
    android:id="@+id/container"
    android:layout_width="match_parent"

android:layout_height="match_parent">
              <ListView 
    android:id="@+id/listview2"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    ></ListView>
        </RelativeLayout>
<RelativeLayout 
    android:layout_below="@id/container"
    android:id="@+id/container1"
    android:layout_width="match_parent"

android:layout_height="match_parent">          

                <ListView 

    android:id="@+id/listview3"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    ></ListView>
 </RelativeLayout>   





<ListView 
    android:id="@+id/drawerlist"
    android:background="#0099CC"
    android:cacheColorHint="#00000000" 

    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="left" ></ListView>

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

Upvotes: 0

Views: 643

Answers (6)

Sampath Kumar
Sampath Kumar

Reputation: 4463

Use LinearLayout with weight attribute.

<LinearLayout
android:id="@+id/container"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ListView
    android:id="@+id/listview2"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="5"/>

<ListView
    android:id="@+id/listview3"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="5" />
</LinearLayout>

Upvotes: 3

0101100101
0101100101

Reputation: 5911

Taken from https://developer.android.com/reference/android/support/v4/widget/DrawerLayout.html

To use a DrawerLayout, position your primary content view as the first child with a width and height of match_parent. Add drawers as child views after the main content view and set the layout_gravity appropriately. Drawers commonly use match_parent for height with a fixed width.

So both your second RelativeLayout and drawerlist are drawers. In any case, it's unclear what exactly you want to achieve, so you have to describe that in more detail. If you want listview3 to come up from the bottom and listview2 to make room for it, you will have to use a different layout or set listview3 to a specific height and animate your first RelativeLayout to exactly that height (using ValueAnimator for bottom margin I'd say).

Edit: After reading a comment of yours, it doesn't seem like something you want to achieve. In that case, what I describe above is the culprit, i.e. the second RelativeLayout being interpreted as a drawer. Make sure you combine your RelativeLayouts to a single layout (preferrably LinearLayout with weights).

Upvotes: 0

RScottCarson
RScottCarson

Reputation: 978

Nest both ListViews in the same relative layout block, one right after the other. Give them both the same weight

<RelativeLayout
    android:id="@+id/container
    ...
    <ListView 
        android:id="@+id/listview2"
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_weight="1"
        ...
    ></ListView>

    <ListView 
        android:id="@+id/listview3"
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_weight="1"
        android:layout_below="@id/listview2"
        ...
    ></ListView>

</RelativeLayout>

I haven't been able to test this, so it may need a few tweaks, but give it a shot.

I would also recommend spending some time formatting your code when you post it because it helps us try to figure out what you're doing.

Upvotes: 0

sjain
sjain

Reputation: 23344

You can use layout_weight property to make both listview same height.

For example, you can follow below hierarchy -

<LinearLayout orientation="vertical">
   <ListView layout_weight=1></ListView>   
   <ListView layout_weight=1></ListView>
</LinearLayout>

Upvotes: 0

Ash
Ash

Reputation: 1461

**Its overlapping because: 1. you are setting the height of the list to : match_parent

  1. There is no reference given in relative layout**

Try this:

<RelativeLayout
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ListView
        android:id="@+id/listview1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"></ListView>

    <ListView
        android:id="@+id/listview2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/listview1"></ListView>

    <ListView

        android:id="@+id/listview3"
        android:layout_below="@id/listview2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        ></ListView>
</RelativeLayout>

Upvotes: 0

Shivam Verma
Shivam Verma

Reputation: 8023

The first RelativeLayout takes the full height of the screen. You'll need to limit the height of the first RelativeLayout otherwise the second RelativeLayout will always be out of the View.

Upvotes: 0

Related Questions