Wasim's answer lead me in the right direction but I had to make some changes to get it working for my current project. I am using this function in a fragment and calling it on button click.
fun showPostDialog(title: String) {
val alert = AlertDialog.Builder(activity)
val edittext = EditText(activity)
edittext.hint = "Enter Name"
edittext.maxLines = 1
var layout = activity?.let { FrameLayout(it) }
//set padding in parent layout
// layout.isPaddingRelative(45,15,45,0)
layout?.setPadding(45,15,45,0)
alert.setTitle(title)
layout?.addView(edittext)
alert.setView(layout)
alert.setPositiveButton(getString(R.string.label_save), DialogInterface.OnClickListener {
dialog, which ->
run {
val qName = edittext.text.toString()
showToast("Posted to leaderboard successfully")
view?.hideKeyboard()
}
})
alert.setNegativeButton(getString(R.string.label_cancel), DialogInterface.OnClickListener {
dialog, which ->
run {
dialog.dismiss()
}
})
alert.show()
}
fun View.hideKeyboard() {
val imm = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm.hideSoftInputFromWindow(windowToken, 0)
}
fun showToast(message: String) {
Toast.makeText(activity, message, Toast.LENGTH_LONG).show()
}
I hope it helps someone else in the near future. Happy coding!