0

I am trying to make a custom dialog alert as shown in the picture; I tried and looked everywhere to make it look like the photo but nothing seems to work. Any help would be appreciated; First picture shows what I want to make and the second what it looks like currently:

enter image description here

enter image description here

class MainActivity : AppCompatActivity() {

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

        val alert = AlertDialog.Builder(this@MainActivity)

        alert.setTitle("Confirm delete")
        alert.setMessage("Lorem ipsum dolor sit amet, consectetuer adipiscing elit.")

        alert.setPositiveButton("DELETE"){dialog, which ->
            Toast.makeText(applicationContext,"Ok, we change the app background.",Toast.LENGTH_SHORT).show()

        }

            alert.setNegativeButton("CANCEL"){dialog,which ->
            Toast.makeText(applicationContext,"You are not agree.",Toast.LENGTH_SHORT).show()
        }

        val dialog: AlertDialog = alert.create()

        dialog.show()
    }
}
rengineer
  • 349
  • 3
  • 19

3 Answers3

2

Alert.Dialog theme depends on your device API levels. In the first picture look like Android KitKat (API 19 - 20) ,in the second picture follows material design pattern (API >20).

If want to dialog like the first picture you may need make custom layout

happy_coding

happy_coding
  • 1,064
  • 1
  • 13
  • 24
0

1.Create Kotlin class for Dialog

class CustomDialog(
    context: Context,val mCallBack: CallBack
) : Dialog(context, R.style.full_screen_dialog) {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.dialog_custom)
        // YOUR COMPONENT HANDLE HERE
    // FOR EXAMPLE
        btn_yes.setOnClickListener {
              mCallBack.onDone()
              dismiss()
        }
        btn_cancel.setOnClickListener {
            dismiss()
        }
    }


    interface CallBack {
        //YOU CAN HANDLE THIS METHOD IN YOUR CALLING ACTIVITY
        fun onDone(rating: Float, comments: String)
    }

}

2.How to call

 var mDialog: CustomDialog = CustomDialog(
            this,
            object : CustomDialog.CallBack {
                override fun onDone() {

                }
            })
        mDialog.show()

3.styles.xml

<style name="full_screen_dialog">
        <item name="android:windowFrame">@null</item>
        <item name="android:windowIsFloating">false</item>
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
        <item name="android:background">@android:color/transparent</item>
        <item name="android:windowBackground">@android:color/transparent</item>
        <item name="colorAccent">@color/transparent</item>
    </style>

Create your own custom layout file and set it on setContentView(R.layout.dialog_custom) as I didn't mentioned here

Kalpesh Rupani
  • 991
  • 4
  • 12
0

This is because your dialog is displaying using material theme. You can use below code to get required result by applying style to AlertDialog.

val alert = AlertDialog.Builder(this@MainActivity,AlertDialog.THEME_HOLO_LIGHT)

Note: this style is deprecated.

Nehal Godhasara
  • 787
  • 3
  • 7