0

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>
Tadej Vengust
  • 1,351
  • 4
  • 18
  • 35

4 Answers4

2

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

Shaheera
  • 307
  • 1
  • 15
  • Yes I did, But i dont want to give it weight since I still want it do dynamically change size depending on elements in listview but I want buttons and edit text to always show – Tadej Vengust Oct 02 '15 at 12:07
  • Check the updated answer. If you set the listview visibility to GONE it will no longer exist in dialog and its height will be auto adjusted with edittext and buttons – Shaheera Oct 02 '15 at 12:19
  • that would work but only when there are no items.. What if there are 2? :/ – Tadej Vengust Oct 02 '15 at 13:03
  • Can you make use of IF ELSE? If(listview has no items) ListView.Visibility to Gone Else ListView.Visibility to Visible – Shaheera Oct 02 '15 at 15:33
1

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);
Syed Arsalan Kazmi
  • 949
  • 1
  • 11
  • 17
  • This is a decent solution but not exactly what I need. Because if there are 100 items I will need to scroll all the way down just do click accept :/ – Tadej Vengust Oct 02 '15 at 14:03
  • Yes, There are such constraints to this solution. The other way around is that you keep your Footer above your ListView and give your footer a top margin as the height of the ListView at runtime and place a check that should not allow the top margin to make the footer exceed the view boundries. – Syed Arsalan Kazmi Oct 06 '15 at 06:57
0

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

  • But I dont want it on top. This question: http://stackoverflow.com/questions/7716635/how-to-prevent-other-view-being-pushed-out-of-the-screen Has picture examples of what I want but I want it in a dialog fragmet in a way that dialog fragment does not strech if not needed – Tadej Vengust Oct 02 '15 at 11:27
0

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.

Tadej Vengust
  • 1,351
  • 4
  • 18
  • 35