I am inflating a custom dialog in my android app. The layout of the dialog is this:
<?xml version="1.0" encoding="utf-8"?>
<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="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/parent"
app:cardBackgroundColor="@android:color/white"
app:cardCornerRadius="15dp"
app:cardElevation="3dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:id="@+id/interior_layout"
android:orientation="vertical">
<ImageView
android:layout_width="80dp"
android:layout_height="80dp"
android:scaleType="centerCrop"
android:adjustViewBounds="true"
android:layout_gravity="center_horizontal"
android:visibility="gone"
android:id="@+id/imageView"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/message"
android:textSize="14sp"
android:textColor="@android:color/black"
android:visibility="gone"
android:layout_marginTop="10dp"
android:text="Hello"
android:layout_gravity="center_horizontal"/>
</LinearLayout>
</androidx.cardview.widget.CardView>
This is my code to inflate the dialog:
Dialog alertDialog = new Dialog(activity);
alertDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialogView = LayoutInflater.from(activity).inflate(R.layout.layout_progress_dialog, null);
alertDialog.setContentView(dialogView);
Objects.requireNonNull(alertDialog.getWindow()).setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
However, the corner radius is not being set. The corners are still sharp. I tried using Alert Dialog but there I faced the problem that it covers the entire width (just like Progress Dialog). With Dialog, I can have the width as much as needed.
You don't need a CardView
to obtain the corner radius.
Just use the getTheme
method:
class RoundedDialog: DialogFragment() {
override fun getTheme() = R.style.RoundedCornersDialog
override fun onCreateView(...): View? {
val v: View = inflater.inflate(R.layout...., container, false)
return v
}
//...
}
with:
<style name="RoundedCornersDialog" parent="@style/Theme.MaterialComponents.Dialog">
<item name="dialogCornerRadius">16dp</item>
</style>
If you want to use a AlertDialog
you can create it in the onCreateDialog
. Check this answer for more details .
You can go for creating a drawable and then making it the background of the view.
You can use the following piece of code to make round corners- just create a new drawable and
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<stroke
android:width="1dp"
android:color="@color/colorPrimaryDark" />
<solid android:color="#2ED771" />
<padding
android:left="1dp"
android:right="1dp"/>
<corners android:radius="15dp" />
</shape>
WIth this you can also specify radius of each corner independently
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.