Search code examples
androidandroid-studiokotlinandroid-dialogfragmentrounded-corners

How to show my dialogFragment as a rounded corners?


Hello i tried to use rounded corners. This picture is last situation of dialogFragment: https://pasteboard.co/IuVANG7.jpg

As you see in the picture, this is not exactly corner rounded. I saw some solutions but it was java codes.i cannot convert java codes to kotlin exactly. :) Here is my codes:

MainActivity:

package com.example.dialogfragment
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

    }


    fun showDialog(view: View) {

        var MyDialog = MyFragment()
        MyDialog.show(supportFragmentManager, "My Dialog")


    }
}

My_dialog.xml:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout 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="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/radius"
    android:orientation="horizontal">


    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:paddingLeft="20dp"
        android:paddingRight="20dp"
        android:text="Bu dialog penceresini beğendiniz mi?"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        tools:layout_editor_absoluteY="16dp" />

    <Button
        android:id="@+id/btnEvet"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/btnHayir"
        android:layout_alignBottom="@+id/btnHayir"
        android:layout_marginStart="0dp"
        android:layout_marginBottom="0dp"
        android:layout_toEndOf="@+id/btnHayir"
        android:text="EVET"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/btnHayir"
        tools:layout_editor_absoluteY="54dp" />

    <Button
        android:id="@+id/btnHayir"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/tv"
        android:layout_alignParentStart="true"
        android:layout_marginStart="41dp"
        android:layout_marginTop="0dp"
        android:text="HAYIR"
        app:layout_constraintEnd_toStartOf="@+id/btnEvet"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        tools:layout_editor_absoluteY="54dp" />
</RelativeLayout>

And my drawable:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="@color/colorAccent"
       />
    <corners android:radius="30dp" />
    <padding
        android:bottom="10dp"
        android:left="10dp"
        android:right="10dp"
        android:top="10dp" />
    </shape>

Solution

  • Add this line to DialogFragment onCreatView and make the transparent background of view.

     dialog.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))