Alex
Alex

Reputation: 258

Android custom dialog displays extra space

My custom dialog adds extra space after the Cancel and Save button. I have made a couple of custom dialogs in this app, but this problem appears only in this case. I've colored the view's background to blue, so I think it should be okay.

enter image description here

layout:

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginTop="@dimen/activity_vertical_margin"
        android:layout_marginLeft="@dimen/activity_horizontal_margin"
        android:layout_marginRight="@dimen/activity_horizontal_margin"
        android:gravity="center_horizontal">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:gravity="center_horizontal">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="@android:color/black"
                android:text="@string/mo"/>
            <CheckBox
                android:id="@+id/cbMo"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>

        </LinearLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:gravity="center_horizontal"
            android:layout_marginLeft="20dp">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="@android:color/black"
                android:text="@string/tu"/>
            <CheckBox
                android:id="@+id/cbTu"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>

        </LinearLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:gravity="center_horizontal"
            android:layout_marginLeft="20dp">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="@android:color/black"
                android:text="@string/we"/>
            <CheckBox
                android:id="@+id/cbWe"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>

        </LinearLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:gravity="center_horizontal"
            android:layout_marginLeft="20dp">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="@android:color/black"
                android:text="@string/th"/>
            <CheckBox
                android:id="@+id/cbTh"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>

        </LinearLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:gravity="center_horizontal"
            android:layout_marginLeft="20dp">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="@android:color/black"
                android:text="@string/fr"/>
            <CheckBox
                android:id="@+id/cbFr"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>

        </LinearLayout>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginLeft="@dimen/activity_horizontal_margin"
        android:layout_marginRight="@dimen/activity_horizontal_margin"
        android:layout_marginTop="@dimen/activity_vertical_margin"
        android:gravity="center_horizontal">

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.4"
            android:layout_marginRight="10dp"
            android:text="@string/repeat_every"
            android:textColor="@android:color/black"
            android:gravity="right"
            android:layout_gravity="center_vertical"/>

        <Spinner
            android:id="@+id/spRecurrence"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.5"
            android:prompt="@string/repeat_every"
            android:layout_gravity="center_vertical"/>

    </LinearLayout>


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginLeft="@dimen/activity_horizontal_margin"
        android:layout_marginRight="@dimen/activity_horizontal_margin"
        android:layout_marginTop="@dimen/activity_vertical_margin">

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.4"
            android:layout_marginRight="10dp"
            android:text="@string/starts_on"
            android:textColor="@android:color/black"
            android:gravity="right"/>

        <TextView
            android:id="@+id/tvDate"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.3"
            android:text="@string/pick_a_date"
            android:drawableRight="@drawable/ic_calendar"
            android:gravity="center_vertical"/>

        <View
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_weight="0.2"
            android:visibility="visible"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginLeft="@dimen/activity_horizontal_margin"
        android:layout_marginRight="@dimen/activity_horizontal_margin"
        android:layout_marginTop="@dimen/activity_vertical_margin">

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.4"
            android:gravity="right"
            android:layout_marginRight="10dp"
            android:textColor="@android:color/black"
            android:text="@string/recurrences_colon"/>

        <EditText
            android:id="@+id/etNumber"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.2"
            android:inputType="number"/>

        <View
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_weight="0.3"
            android:visibility="invisible"/>
    </LinearLayout>

    <TextView
        android:id="@+id/tvError"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="@android:color/holo_red_dark"
        android:visibility="visible"
        android:text="Error text"/>

</LinearLayout>

Displaying the dialog:

Context context = getContext();
        final NewRecurrencesDialogView recurrencesDialogView = new NewRecurrencesDialogView(context, subject);
        AlertDialog.Builder builder = new AlertDialog.Builder(context);

        builder.setView(recurrencesDialogView)
                .setTitle(R.string.set_recurrence)
                .setPositiveButton(R.string.save, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {    }
                })
                .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                    }
                });

        final Dialog dialog = builder.create();
        dialog.setCancelable(false);
        dialog.show();
        ((AlertDialog)dialog).getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ArrayList<Teaching> newRecurrences = recurrencesDialogView.saveRecurrences();

                if (newRecurrences != null) {
                    recurrences.addAll(newRecurrences);

                    Collections.sort(recurrences, new Comparator<Teaching>() {
                        @Override
                        public int compare(Teaching lhs, Teaching rhs) {
                            if (lhs.getBegin() < rhs.getBegin())
                                return -1;
                            else
                                return 1;
                        }
                    });

                    updateListViewAdapter();
                    dataChangedListener.setChanged(true);
                    dialog.dismiss();
                }
            }
        });
        ((AlertDialog)dialog).getButton(AlertDialog.BUTTON_NEUTRAL).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });

I've compared the layout file with the other dialog's files, but can't figure out where the problem is.

Upvotes: 1

Views: 570

Answers (1)

Jiyeh
Jiyeh

Reputation: 5297

I think the problem is with the android:layout_weight you set for your view.

Try change your:

<View
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_weight="0.2"
    android:visibility="visible"/>

To this:

<View
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="0.2"
    android:visibility="visible"/>

I am not really sure what you trying to achieve, but think this will show you the direction (hopefully the right one).

Upvotes: 1

Related Questions