Home > Mobile >  .setOnShowListener not appearing on fragment
.setOnShowListener not appearing on fragment

Time:09-10

I'm new to android and I've watched tutorials online on creating alert dialogs with countdowns but i can't seem to use the .setOnShowListener on my alert box.

I made this on a Fragment

Output Reference

I tried to transfer it to the homepage activity but the button is on the 1st fragment

CodePudding user response:

This might solve your problem.

val builder = AlertDialog.Builder(requireContext())
        binding = FragmentFirstBinding.inflate(layoutInflater)

        binding.button2.setOnClickListener {
            builder.setTitle("Alert!")
            builder.setMessage("Are u OK?")
            builder.setCancelable(true)
            builder.setPositiveButton("yes") {
                    dialog, which ->
                    //do something
                }
            builder.setNegativeButton("No") {
                    dialog,which ->
                    //do something
                }
            builder.create().setOnShowListener { 
                //do something
            }
            builder.show()
        }

You cannot call setOnShowListener() method without calling create() first

Please mark it as the answer if it solved your problem, or ask me where it's going wrong after this.

Hope this helps! :)

CodePudding user response:

It's wrong call of the method, try this

builder.setOnShowListener(this);

Or you can simply write your call inside the method like this

 builder.setOnShowListener(new DialogInterface.OnShowListener() {

  @Override
public void onShow(DialogInterface dialog) {
  // your code
}

});

  • Related