androidandroid-stylesandroid-dialog

How to remove the white corners of Dialog(Not AlertDialog nor DialogFragment)?


I created line_dialog_border.xml like this:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
       android:shape="rectangle">
    <solid android:color="@color/white"/>
    <corners android:radius="20dp"/>
    <stroke
            android:width="5dp"
            android:color="@android:color/black"/>
</shape>

enter image description here

And this is a dialog in kt

private fun showInternetCheckDialog(context: Context) {
    internetCheckDialog = Dialog(context)
    internetCheckDialog.setContentView(R.layout.dialog_internet_check)
    internetCheckDialog.window!!.requestFeature(Window.FEATURE_NO_TITLE)
    internetCheckDialog.window!!
          .setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
    internetCheckDialog.window!!
          .decorView.setBackgroundResource(R.drawable.line_dialogue_border)
    internetCheckDialog.setCanceledOnTouchOutside(false)

    val btnOk = internetCheckDialog.btn_ok

    btnOk.setOnClickListener {
        internetCheckDialog.dismiss()
    }

    try {
        if (!internetCheckDialog.isShowing) {
            internetCheckDialog.show()
        }
    } catch (e: Exception) {
        e.printStackTrace()
    }
}

There are many questions like this. But I just see AlertDialog or DialogFragment.

I've tried with

dialog.window!!
    .setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
dialog.window!!
    .decorView.setBackgroundResource(R.drawable.line_dialogue_border)
dialog.window!!.setBackgroundDrawableResource(android.R.color.transparent);

And this is layout xml. I just give a background attribute with the xml that I created above.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/line_dialog_border"
        android:padding="20dp"
        android:orientation="vertical">

    <TextView
            android:id="@+id/tv_message"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:textColor="@android:color/black"
            android:lineSpacingExtra="10dp"
            android:text="No\nInternet\nConnection"
            android:textAlignment="center"
            android:textSize="24sp"
    />
</RelativeLayout>

But nothing worked out. What should I do?


Solution

  • Maybe you should Correct setting dialog window or create custom style.xml

    Window window=this.getWindow();
    window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    

    style.xml:

    <style name="DialogActivityStyle">
    <item name="android:background">@android:color/white</item>
    <item name="android:windowBackground">@android:color/transparent</item>
    </style>
    

    AndroidMainfest:

    <activity android:name=".YourActivity"
              android:theme="@style/DialogActivityStyle"/>