Dialog

Dialog

 

⇒ we can create custom dialog popups instead of showing default Alert -dialog box :

  • we create custom dialog with Dialog class
  • setContentView() set layout view of our dialog box
 
package com.example.demoapi import android.app.Dialog import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import android.widget.Button import android.widget.Toast class dialogg : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_dialogg) // to crate new custom dialog box val dialog = Dialog(this@dialogg) // set layout to our dialog box dialog.setContentView(R.layout.dialog_box) // create a button from dialog object (not from current activity) val okBtn : Button = dialog.findViewById(R.id.okbtn) okBtn.setOnClickListener{ Toast.makeText(this@dialogg, "Text Message", Toast.LENGTH_SHORT).show() dialog.dismiss() } // to show dialog dialog.show() } }
 
 

Set Background Transparent of Dialog

dialog.getWindow()?.setBackgroundDrawableResource(android.R.color.transparent)