1

I'm trying to code a dialogFragment which contains a ListView and a button below that ListView. The button must be always visible, this means that you can scroll the ListView but the button remains always visible.

The Dialog must also adapt its height. That means that if the listView contains only fews elements (1 or 2 elements) the DialogFragment should not fill the entire height of the screen...

This is my code:

<?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">
    <LinearLayout
        android:orientation="vertical"
        android:padding="@dimen/fragment_default_padding"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <ImageView
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:src="@drawable/Back_arrow"
            android:id="@+id/imgBack" />
        <TextView
            android:text="@string/this_pizzeria_require_a_phone_number_inorderto_accept_orders"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            style="@style/default_title" />
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/edtPhoneNumber"
            android:layout_marginBottom="4dp"
            android:singleLine="true"
            android:imeOptions="actionDone"
            android:hint="@string/choose_from_the_below_list_or_insert_a_new_one_here"
            android:inputType="phone" />
        <ListView
            android:id="@+id/lvPhones"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_above="@+id/btnAdd"
            android:dividerHeight="1dp" />
        <Button
            android:id="@+id/btnDone"
            android:text="@string/done"
            style="@style/default_button" />
    </LinearLayout>
</RelativeLayout>

This code works fine, but if the listView contains a lot of items, the button below disappear. This is a screen of the dialog: enter image description here

As you can see the button is stretched, and adding another item to the listView, it disappears.

Is there a way to achieve my goal?

Thanks a lot!

user3471528
  • 3,013
  • 6
  • 36
  • 60

3 Answers3

0

I don't personally care all that much for your layout. I think you should split it up a bit better in your linearLayout. However whenever I am faced with this problem I add

android:layout_weight="1"

and it is generally resolved.

    <Button
        android:id="@+id/btnDone"
        android:text="@string/done"
        android:layout_weight="1"
        style="@style/default_button" />
RyPope
  • 2,645
  • 27
  • 51
0

I normally use a RelativeLayout for that purpose. You have a lot more control over how your elements are arranged.

For whatever reason (I never investigated it, using RelativeLayout works without losing time) ListViews will always extend to the bottom in LinearLayout (by purpose I think)

In your case you could just move the button from the linear to the container-view i think (which is a relative layout) and align to bottom - that topmost RelativeLayout is useless anyway in your code :-)

reiti.net
  • 315
  • 2
  • 12
0

Use AlertDialog.Builder with setView() pointing to your layout with button removed and setPositiveButton() as your "done" button. This way button is always at the bottom and dialog adapts it height automatically.

public class YourDialogFragment extends DialogFragment implements ... {
    ...
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog dialog = new AlertDialog.Builder(getActivity())
            .setTitle("login")
            .setView(createDialogView())
            .setPositiveButton(R.string.done, this)
            .create();
        dialog.setCancelable(true);
        return dialog;
    }

    private View createDialogView() {
        final LayoutInflater inflater = getActivity().getLayoutInflater();
        View contentView = inflater.inflate(R.layout.content, null);

        ...
        ListView listView = (ListView) contentView.findViewById(R.id.lvPhones);
        listView.setAdapter(adapter);
    }
biegleux
  • 13,179
  • 11
  • 45
  • 52