0

title saying what I need

I need to AlertDialog will not close after I click on positive button because I need show a message in that AlertDialog when user click on positive button

a have found this Stop AlertDialog from closing on positive button click but I can't use it in Kotlin

  alertDialog.setPositiveButton("ok"){ dialogInterface, i ->
        if(myET.text.toString().length !in 10..100){
            myET.error = "text size not in the range"
            //here must not close
        }else{
            myfunction()
            // only here must close
        }
  }
  • Onclick performs an action that you defined. After performing that action dialog box has to closed, you can show your message or another dialog box on Ok button clicked. When the ok button is clicked then another dialog or message you defined will prompt. hope it clears now – Shoaib Kakal Aug 21 '20 at 11:40

2 Answers2

0

Use an AlertDilaog with a custom view.

val dialogBuilder: AlertDialog.Builder = AlertDialog.Builder(this)
    val inflater = this.layoutInflater
    val dialogView: View = inflater.inflate(R.layout.custom_dialog_layout, null)
    dialogBuilder.setView(dialogView)

    val positiveButton =
        dialogView.findViewById<Button>(R.id.positive_button) as EditText

    val alertDialog: AlertDialog = dialogBuilder.create()
    alertDialog.show()
    
    positiveButton.setOnClickListener { 
        //do whatever you have to do here

        //dismiss dialog when done
        alertDialog.dismiss()
}
Oshan Madusanka
  • 327
  • 3
  • 9
0

Try below code hope it works

 final AlertDialog dialog = new AlertDialog.Builder(context)
    .setView(v)
    .setTitle(R.string.my_title)
    .setPositiveButton(android.R.string.ok, null) 
    .setNegativeButton(android.R.string.cancel, null)
    .create();

 dialog.setOnShowListener(new DialogInterface.OnShowListener() {

@Override
public void onShow(DialogInterface dialogInterface) {

    Button button = ((AlertDialog) dialog).getButton(AlertDialog.BUTTON_POSITIVE);
    button.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            // TODO Do something

            //Dismiss once everything is OK.
            dialog.dismiss();
        }
    });
}
});
dialog.show();
sumit singh
  • 588
  • 1
  • 5
  • 20