0

Is there a way to animate an existing open dialog/alert dialog ? I would like to mimic how Apple deals with their dialog such that when a user enters and invalid credentials, the dialog would vibrate without it having to be dismiss and show again.

The closest I got was animating the editText box with the shake animation found here: Link

 val dialog = Dialog(this)
        dialog.setContentView(R.layout.authentication_dialog)
        val authenticator = dialog.findViewById<Button>(R.id.submit_auth_phone)
        var phoneNumber = dialog.findViewById<EditText>(R.id.auth_phone)
        val alertText = dialog.findViewById<TextView>(R.id.alertText)

        dialog.setCancelable(false)
        dialog.setCanceledOnTouchOutside(false)
        dialog.show()


        authenticator.setOnClickListener {

            AuthAsyncTask(feTable).execute()
            if (phoneNumber.text.toString().length < 8){
                val shake = AnimationUtils.loadAnimation(this, R.anim.shake)

                phoneNumber.startAnimation(shake)
                alertText.visibility = TextView.VISIBLE


            }
}

video

As you can see from the video, the dialog will "shake" when I click a button whereas the tutorials from everywhere is animate upon opening or dismissing their dialog

Shreamy
  • 341
  • 2
  • 16

2 Answers2

0

Dialog(requireContext()).window.decorView try to animate this view. if even it doesnt work I would suggest to use DialogFragment as workaround

0

Here a solution in Kotlin creating an extension function for Window:

fun Window.shake() {
    val v1 = 1.dp
    val s2 = 3.dp
    val v3 = 8.dp
    val animator = ObjectAnimator
            .ofInt(attributes, object : Property<WindowManager.LayoutParams, Int>(Int::class.java, "x") {
                override fun get(o: WindowManager.LayoutParams): Int {
                    return o.x
                }

                override fun set(o: WindowManager.LayoutParams, value: Int) {
                    o.x = value
                }
            }, 0, v3, -v3, v3, -v3, s2, -s2, v1, -v1, 0)
            .setDuration(500)
    animator.addUpdateListener {
        attributes = attributes
    }
    animator.start()
}

You can then use it like this:

dialog.window?.shake()
Oliver Jonas
  • 1,188
  • 14
  • 12