Tadej Vengust
Tadej Vengust

Reputation: 1401

Listview in dialog dynamic layout

I have a simple fragment dialog with a listview, EditText and two button (accept and cancel).

I want my layout to look like this: Listview on top EditText right below and Buttons side by side below edittext.

Now my problem is that listview can have 0 or a 100 elements. So if I put everythis in a vertical LinearLayout and listview has a lot of elements the edittext and buttons get pushed out of view. And if I use relative layout and say that edit text is aligned parent bottom and below the listview that it looks ok for 100elements but when the listview is empty the dialog uses all of the screen space.

Here is an example of my code when using relativeLayout. Is there another way to make is so that linearLayout with id "below" is below the listview but will still be visible(if list view has a lot of items) without using alignParentBottom="true" because that is the reason the layout stretches to full screen.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/ListViewPreNotes"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/bottom" />


    <LinearLayout
        android:id="@+id/below"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_alignParentBottom="true">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="left"
            android:padding="3dp">

            <TextView
                android:layout_height="@dimen/label_height"
                android:layout_width="130dip"
                android:text="@string/note"
                android:layout_marginLeft="10dip"
                android:gravity="center_vertical"
                android:textSize="@dimen/text_size_large"/>

            <EditText
                android:id="@+id/OMnote"
                android:layout_height="wrap_content"
                android:layout_width="200dp"
                android:textSize="@dimen/text_size_large"
                android:inputType="textNoSuggestions|textCapSentences"
                android:selectAllOnFocus="true"
                android:hint="@string/note"/>

        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:baselineAligned="false">

            <Button
                android:id="@+id/dialogButtonClose"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="50dp"
                android:text="@string/ok"
                style="?android:attr/borderlessButtonStyle"
                android:textStyle="bold" />
            <Button
                android:id="@+id/dialogButtonCancel"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="50dp"
                android:text="@string/cancel"
                android:textStyle="bold"
                style="?android:attr/borderlessButtonStyle" />

        </LinearLayout>

    </LinearLayout>

</RelativeLayout>

Upvotes: 0

Views: 556

Answers (4)

Tadej Vengust
Tadej Vengust

Reputation: 1401

Some credit goes to Arsalan Shah for his suggestions.

This is the final version of how I solved my problem (kind of hacky):

First I check how many elements I have in a list view then depending on that and depending on the device and orientation mode (portrait or landscape) I inflate eather the layout in my question or another layout with listview footer that Arsalan Shah suggested.

Example: If the device is below 7" and it is in landscape mode and the number of items in my list is above 4 I will use my layout otherwise I will use Arsalan Shah suggestion. I sort of tested for what the number of items on which layout/device should be for what layout to find the best case scenario for my design.

Hope this helps anyone else that might have the same problem. If anyone has a suggestion how to do all this in only one layout then please comment below.

Upvotes: 0

Syed Arsalan Kazmi
Syed Arsalan Kazmi

Reputation: 1285

you can add what you need to show below the ListView in its footer. You can add it like this.

View footerView = ((LayoutInflater) ActivityContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.footer_layout, null, false);
ListView.addFooterView(footerView);

Upvotes: 1

Shaheera
Shaheera

Reputation: 307

Set ListView.Visibility to Gone when no records found and use RelativeLayout and align parent bottom.

Upvotes: 2

Mudit Dangi
Mudit Dangi

Reputation: 46

You can use FrameLayout as the root and then you can place your LinearLayouts on top of the ListView. http://developer.android.com/reference/android/widget/FrameLayout.html

Upvotes: 0

Related Questions