4

I am trying to add a border for a dialog window in Android. Have added rounded corners to the dialog window using a drawable with the following lines

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:left="5dp"
        android:right="5dp"
        android:bottom="5dp"
        android:top="5dp">
    <shape android:shape="rectangle">
        <solid android:color="#ffbf80" />
        <corners android:radius="30dp" />
        <padding
        android:left="1dp"
        android:top="1dp"
        android:right="1dp"
        android:bottom="1dp" />
    </shape>
    </item>
</layer-list>

To add border to the dialog box, I tried by adding the stroke element as below

<shape android:shape="rectangle">
    <solid
    android:color="#ffbf80" />
    <stroke android:color="#ff3300" android:width="2dp"/>
    <corners
    android:radius="30dp" />
    <padding
    android:left="1dp"
    android:top="1dp"
    android:right="1dp"
    android:bottom="1dp" />
</shape>

But this is creating a border around every element in the dialog like textview, icon, button also. I just want an outline border for the window.

I have created the alert dialog in the java code using AlertDialog.Builder,

myDialogBuilder=new AlertDialog.Builder(new 
ContextThemeWrapper(getActivity(), 
R.style.CustomDialog))
.setTitle(title.getText().toString())
.setMessage(myText)
.setPositiveButton("Done", new DialogInterface.OnClickListener() {
              public void onClick(DialogInterface dialog, int which)
              {
                 getDialog().dismiss();
              }
              });

myDialog=myDialogBuilder.create();

myDialog.setOnShowListener(new DialogInterface.OnShowListener() {
                @Override
                public void onShow(DialogInterface dialog) {
                    Window view=((AlertDialog)dialog).getWindow();
                    view.setBackgroundDrawable(new 
                    ColorDrawable(Color.TRANSPARENT));
                ...
                }
 });

I have the following in my styles.xml for the dialog box

 <style name="CustomDialog" parent="@style/ThemeOverlay.AppCompat.Dialog">
    <item name="android:backgroundDimEnabled">true</item>
    <item name="android:background">@drawable/dialog_bg</item>
    <item name="android:textColorPrimary">@color/my_textcolor</item>
</style>

Please help me understand what changes should I make so that the alert dialog border gets displayed.

Thanks

Sri
  • 537
  • 1
  • 5
  • 11

2 Answers2

5

I was able to solve the border issue for my alert dialog by making the following changes in the code.

Instead of specifying the theme when creating the dialog, I added the drawable resource to the dialog window in dialog.setOnShowListener()

Window view=((AlertDialog)dialog).getWindow();
view.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
// to get rounded corners and border for dialog window
view.setBackgroundDrawableResource(R.drawable.dialog_bg);

I just need a title, message and one button in my alert dialog. So I am using the standard AlertDialog.Builder way of creating it, though it can also be done using a custom layout file and setting the view.

Sri
  • 537
  • 1
  • 5
  • 11
0

I have rounded corner dialogs in my app, but I use custom dialogs with custom layout and not the standard AlertDialog, of course it need much more work.

  • Define your MyDialogStyle in styles.xml with :
    <item name="android:background">@drawable/my_rounded_background</item>

  • In the root item of the dialog layout xml add the attribute :
    style="@style/MyDialogStyle"

  • In your Dialog class onCreate add:

getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

from56
  • 3,976
  • 2
  • 13
  • 23
  • Thanks for your response. I have already added the three things that you mentioned but the only difference is that I am creating the alert dialog in code instead of using a custom layout. Is that why it is not working? – Sri Oct 15 '17 at 03:44
  • if you are still using "myDialogBuilder = new AlertDialog.Builder(..)" then you are not creating a custom Dialog and the class you created for the custom dialog does nothing because you are not using it. Change it to "myDialog = new MyCustomDialog()" or whatever you called your custom dialog class. To create a custom dialog means create a Java class that extends Dialog Android class. – from56 Oct 15 '17 at 10:24
  • I think for api 11 and above we can create alert dialog with custom style using AlertDialog.Builder. Please check this link https://stackoverflow.com/questions/14770400/android-alertdialog-styling. I have been able to set background-color and rounded corners for the alert dialog but it is the stroke alone that is not working. When I add stroke in shape in drawable file, a rounded border is getting displayed for the title, button, message and everything that is there in the alert dialog. I just need a single outline border for the alert dialog box. – Sri Oct 17 '17 at 21:26
  • Custom style does not mean custom Dialog. If you use AlertDialog.Builder you are not using a custom dialog, you are using a standard dialog. A custom dialog is constructed similar to an Activity with a Java class and a XML layout, this is what I used for rounded corner dialog. The solution on the link you said is really complicated, bizarre, and in the comments some guy said it doesn't work for him. – from56 Oct 17 '17 at 23:07