0

I have an AlertDialog that I set a custom view to. In Android Studio's layout preview it shows what I expect the layout to look like:

enter image description here

However when the app runs on my Nexus 6P it looks like this (Note the extra space at the top, that is what I do not want):

enter image description here

As suggested here I tried changing how the layout was set to:

alertDialog2.setView(view2,0,0,0,0);
alertDialog2.show();

However this did not solve my problem. Any suggestions for how to accomplish this?

XML Layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">

<TextView
    android:text="Suggest improvements or changes, or report bugs."
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="20dp"
    android:id="@+id/textView5"
    android:textColor="@color/Black"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp" />

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="45dp">

    <EditText
        android:layout_height="wrap_content"
        android:inputType="textEmailAddress"
        android:ems="10"
        android:id="@+id/email_feedback_text"
        android:layout_marginLeft="125dp"
        android:layout_width="250dp"
        android:layout_alignParentBottom="false"
        android:layout_marginRight="5dp" />

    <TextView
        android:text="E-mail*:"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp"
        android:id="@+id/textV5"
        android:textColor="@color/Black"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="30dp" />
</RelativeLayout>
</RelativeLayout>

Java:

          AlertDialog.Builder alertDialogBuilderComplete = new AlertDialog.Builder(
                                MainActivity.this);

                        alertDialogBuilderComplete.setCancelable(false);
                        alertDialogBuilderComplete.setTitle("Submit Feedback"); //Set the title of the box
                        alertDialogBuilderComplete.setMessage("");
                        alertDialogBuilderComplete.setNegativeButton("Submit",null);
                        alertDialogBuilderComplete.setPositiveButton("Cancel", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                dialog.cancel(); //when they click dismiss we will dismiss the box
                            }
                        });
                        alertDialog2 = alertDialogBuilderComplete.create(); //create the box
                        alertDialog2.setOnShowListener(new DialogInterface.OnShowListener(){
                            @Override
                            public void onShow(DialogInterface dialog) {
                                Button button = ((AlertDialog) dialog).getButton(AlertDialog.BUTTON_NEGATIVE);
                                button.setOnClickListener(new View.OnClickListener() {
                                    }
                                });
                            }
                        });

                        alertDialog2.setView(view2,0,0,0,0);
                        alertDialog2.show(); 
Community
  • 1
  • 1
ez4nick
  • 9,756
  • 12
  • 37
  • 69

3 Answers3

2

Remove this line:

alertDialogBuilderComplete.setMessage("")

Or change it to:

alertDialogBuilderComplete.setMessage(null)
user5968678
  • 2,074
  • 1
  • 15
  • 17
  • It works great but when I do want to have a message there is still a gap under the message. Is there a way to remove it? – Idan Dec 22 '20 at 11:55
0

After you posted your xml, I see android:layout_marginTop="20dp" make it android:layout_marginTop="0dp"

<TextView
    android:text="Suggest improvements or changes, or report bugs."
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="0dp"
    android:id="@+id/textView5"
    android:textColor="@color/Black"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp" />
Amey Shirke
  • 599
  • 1
  • 5
  • 16
  • Tried that and same issue as before. I am referring to the space below Submit Feedback and on top of Suggest Improvements – ez4nick Jan 11 '17 at 15:35
  • Which extra space you referring to? Above submit feedback? or above suggest improvements..? – Amey Shirke Jan 11 '17 at 15:37
0
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">

<TextView
android:text="Suggest improvements or changes, or report bugs."
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
// remove margin top
android:id="@+id/textView5"
android:textColor="@color/Black"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp" />
  ........
         .......
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
// remove margin top, can be added margin as needed

<EditText
    android:layout_height="wrap_content"
    android:inputType="textEmailAddress"
    android:ems="10"
    android:id="@+id/email_feedback_text"
    android:layout_marginLeft="125dp"
    android:layout_width="250dp"
    android:layout_alignParentBottom="false"
    android:layout_marginRight="5dp" />
     ......
           ..........
     </RelativeLayout>
W4R10CK
  • 5,502
  • 2
  • 19
  • 30