AlertDialog: How To Remove Black Borders Above and Below View

22,010

Solution 1

If you look at the AlertDialog class source you'll see most of the methods are simply proxy methods (facade) around private AlertController mAlert.

Looking at the AlertController class source you'll see 4 interesting member variables:

private int mViewSpacingLeft;
private int mViewSpacingTop;
private int mViewSpacingRight;
private int mViewSpacingBottom;
private boolean mViewSpacingSpecified = false;

Setting mViewSpacingSpecified to true will remove the borders on the top and bottom of the dialog.

This is done properly by changing this line:

dialog.setView(layout);

to:

dialog.setView(layout, 0, 0, 0, 0);

Solution 2

dialog.setInverseBackgroundForced(true);

use the above in your code to remove the border of the alert dialog.

Refer this LINK for InverseBackgroundForced.

UPDATED Try this code::::

public class Welcome  extends Activity
{
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.welcome);

        AlertDialog.Builder builder = new AlertDialog.Builder(Welcome.this);
        LayoutInflater _inflater = LayoutInflater.from(Welcome.this);
        View view = _inflater.inflate(R.layout.welcomedialog,null);
        builder.setView(view);

        AlertDialog alert = builder.create();
        alert.show();
    }
}

Note:: Also try by removing android:padding="40px" from welcomedialog.xml.

Solution 3

In my case, that border was caused by the theme of the parent Activity for the AlertDialog. To get rid of the border completely, give it a different theme (in this case, Holo):

AlertDialog.Builder builder = new AlertDialog.Builder(
                                new ContextThemeWrapper(this, android.R.style.Theme_Holo)
                              );

This fixed it for me. Hope this helps!

Solution 4

Just to make Steve's answer more clear, this can be done easily. For example in my case the view I was setting in the dialog was a WebView.

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

    WebView webView = new WebView(getActivity());
    webView.loadUrl(" url to show ");


    OnClickListener clickListenerOk = new OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            ...
        }
    };

    OnClickListener clickListenerCancel = new OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            ...
        }
    };

    AlertDialog dialog = new AlertDialog.Builder(getActivity())

            .setPositiveButton("OK", clickListenerOk)

            .setNegativeButton("Cancel",clickListenerCancel)

            .create();

    dialog.setView(webView, 0, 0, 0, 0);

    return dialog;
}
Share:
22,010
Steve
Author by

Steve

Opinions are my own and do not represent the views of my employer or anyone else.

Updated on July 16, 2022

Comments

  • Steve
    Steve almost 2 years

    This question has been asked before: AlertDialog custom title has black border

    But was not answered satisfactorily - and is missing some information.


    I'm trying to create a custom dialog in Android without a title and without any buttons along the bottom.

    However, the resulting dialog has black "borders"/"spacing"/something along the top and bottom of the view.

    From the Documentation:

    A dialog made with the base Dialog class must have a title. If you don't call setTitle(), then the space used for the title remains empty, but still visible. If you don't want a title at all, then you should create your custom dialog using the AlertDialog class. However, because an AlertDialog is created easiest with the AlertDialog.Builder class, you do not have access to the setContentView(int) method used above. Instead, you must use setView(View). This method accepts a View object, so you need to inflate the layout's root View object from XML.

    So, that's what I did:

    Welcome.java

    public class Welcome  extends Activity
    {
        public void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
    
            setContentView(R.layout.welcome);
    
            LayoutInflater inflater = (LayoutInflater)this.getSystemService(LAYOUT_INFLATER_SERVICE);
            View layout = inflater.inflate(R.layout.welcomedialog, (ViewGroup)findViewById(R.id.layout_root));
    
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setView(layout);
            builder.create().show();
        }
    }
    

    welcomedialog.xml

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                  android:layout_width="fill_parent"
                  android:layout_height="fill_parent"
                  android:background="@drawable/texturebg"
                  android:id="@+id/layout_root"
                  android:orientation="vertical"
                  android:padding="40px">
        ...
    </LinearLayout>
    

    NOTE: I've tried using FrameLayout as the root ViewGroup instead of LinearLayout as per a suggestion I found somewhere - but that didn't help.

    Result

    enter image description here enter image description here


    setBackgroundDrawable Suggestion

    public class Welcome  extends Activity
    {
        public void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
    
            setContentView(R.layout.welcome);
    
            LayoutInflater inflater = (LayoutInflater)this.getSystemService(LAYOUT_INFLATER_SERVICE);
            View layout = inflater.inflate(R.layout.welcomedialog, (ViewGroup)findViewById(R.id.layout_root));
    
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setView(layout);
            AlertDialog dialog = builder.create();
    
            dialog.getWindow().setBackgroundDrawable(new ColorDrawable(0));
    
            dialog.show();
        }
    }
    

    Didn't work for me.

    • Fakebear
      Fakebear almost 12 years
      Can this code use setPositiveButton method? If I the new view I made in layout has buttons, how to set onClickListener callback?
    • Steve
      Steve almost 12 years
      @Fakebear, I think your comment/question is outside the scope of this question. You'll probably want to search for that and/or start a new question.
  • Steve
    Steve about 12 years
    On the dialog's window or view?
  • Steve
    Steve about 12 years
    This changed the black to white - but still leaves the space.
  • Steve
    Steve about 12 years
    False makes the background black - but still leaves the space.
  • Matt Robertson
    Matt Robertson almost 12 years
    Worked for me with this small edit, thanks! AlertDialog dialog = builder.create(); dialog.setView(layout,0,0,0,0); dialog.show();
  • antoniom
    antoniom almost 11 years
    @Steve a big THANK YOU! After hours of research, dealing with custom theming and styling I finally found your answer which solved my problem!
  • Aron Lorincz
    Aron Lorincz almost 10 years
    Some small border still stays here, but better.
  • KK_07k11A0585
    KK_07k11A0585 almost 10 years
    @Steve many thanks i have been fighting with this issue for hours... :)
  • Prashanth Debbadwar
    Prashanth Debbadwar about 9 years
    it showing this error "The method setView(int) in the type AlertDialog.Builder is not applicable for the arguments (View, int, int, int, int)"
  • Ariel Capozzoli
    Ariel Capozzoli about 9 years
    He is not aplying it to the builder, he is aplying it to the dialog itself
  • Michael
    Michael almost 9 years
    This is the solution that worked for me, using a custom DialogFragment