Reputation: 7433
I have successfully tried to make a custom alert dialog using the codes below. However, when I tried to make the Constraint Layout
in custom_dialog.xml
to have rounded corners, it showed a fault: the black (darkened/tinted) area does not cover the areas that are white due to the rounded corners. This is what I'm talking about:
My code for MainActivity.kt
:
package com.example.customdialog
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import androidx.appcompat.app.AlertDialog
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.custom_dialog.view.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
fun showDialog(view: View) {
val mDialogView = LayoutInflater.from(this).inflate(R.layout.custom_dialog, null)
val mBuilder = AlertDialog.Builder(this)
.setView(mDialogView)
val mAlertDialog = mBuilder.show()
mDialogView.closeDialog.setOnClickListener {
mAlertDialog.dismiss()
}
}
}
My code for custom_dialog.xml
:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/rounded_corners">
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="141dp"
android:layout_marginLeft="141dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="142dp"
android:layout_marginRight="142dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:src="@mipmap/smiley_foreground"
android:tint="#FFF"/>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="WORKED!"
android:textColor="#FFF"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imageView" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="Apparently, the custom dialog worked"
android:textColor="#FFF"
android:textSize="18sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView2" />
<Button
android:id="@+id/closeDialog"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="16dp"
android:background="@drawable/rounded_button"
android:backgroundTint="#FFF"
android:text="Close Dialog"
android:textColor="@color/colorPrimary"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView3" />
</androidx.constraintlayout.widget.ConstraintLayout>
My code to make the Constraint Layout
from custom_dialog.xml
rounded:
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@color/colorPrimary"/>
<corners android:radius="25dp"/>
<padding android:left="0dp" android:top="0dp" android:right="0dp" android:bottom="0dp" />
</shape>
How do I remove the white area around the dialog?
Upvotes: 0
Views: 109
Reputation: 969
I had the same problem. Making the background transparent can solve this problem .
<style name="Theme_Dialog" parent="Theme.AppCompat.Light.Dialog">
<item name="android:background">@android:color/transparent</item>
</style>
Add this style while initialing your dialog .
val dialog = Dialog(context, R.style.Theme_Dialog)
Not sure about your @drawable/rounded_corners". Instead, you can have card view and put this constraint layout inside card view . With app:cardCornerRadius="@dimen/space_18" you have set the radius as required .
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:toots="http://schemas.android.com/tools"
android:orientation="vertical"
android:background="@color/white_color"
app:cardCornerRadius="@dimen/space_18">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
..
</androidx.constraintlayout.widget.ConstraintLayout>
Upvotes: 1
Reputation: 305
You can easily supply your own style:
<style name="MyDialogTheme" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="android:background">@color/transparent</item>
</style>
and then apply it in the Builder constructor:
AlertDialog alertDialog = new AlertDialog.Builder(getContext(), R.style.MyDialogTheme)
...
.create();
Upvotes: 0