Auto-Droid ツ
Auto-Droid ツ

Reputation: 1593

Add load animation to custom dialog in kotlin

I want to add load animation to custom dialog, I was able to do it in java but I m having issue to do the same in kotlin it gives me the below error

E/AndroidRuntime: FATAL EXCEPTION: main
android.util.AndroidRuntimeException: requestFeature() must be called before adding content
at com.android.internal.policy.impl.PhoneWindow.requestFeature(PhoneWindow.java:229)
at com.android.internal.app.AlertController.installContent(AlertController.java:234)
at android.app.AlertDialog.onCreate(AlertDialog.java:337)
at android.app.Dialog.dispatchOnCreate(Dialog.java:355)
at android.app.Dialog.show(Dialog.java:260)
at com.autodroid.demo.MainActivity.show(MainActivity.kt:66)
at com.autodroid.demo.MainActivity$onCreate$1.onClick(MainActivity.kt:32)
at android.view.View.performClick(View.java:4204)
at android.view.View$PerformClick.run(View.java:17355)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)

Below is my code to create a dialog in kotlin

 val dialogBuilder = AlertDialog.Builder(activity)
        val inflater = activity?.layoutInflater
        val dialogView = inflater?.inflate(R.layout.newly_dialog, null)
        dialogBuilder.setView(dialogView)

        val tvPopText = dialogView?.findViewById<View>(R.id.tvPopText) as TextView

        val dBuilder = dialogBuilder.create()

        //-----> Issue  :( 
        var viewGroup = dBuilder.window.decorView
        viewGroup.startAnimation(AnimationUtils.loadAnimation(activity, R.anim.slide_down))
      //-----> Issue

        dBuilder.show()

Any pointer will be appreciated , Thanks in advance :)

Upvotes: 0

Views: 2504

Answers (1)

ak sacha
ak sacha

Reputation: 2179

style.xml

<style name="yourCustomDialog" parent="@android:style/Theme.Dialog">
         <item name="android:windowAnimationStyle">@style/yourCustomDialogAnimation</item>
     </style>

     <style name="yourCustomDialogAnimation">
        <item name="android:windowEnterAnimation">@anim/slide_down</item>
        <item name="android:windowExitAnimation">@anim/slide_up</item>
     </style>

 </resources>

How to use it in kotlin code

val dialogBuilder = AlertDialog.Builder(activity,R.style.yourCustomDialog)

Upvotes: 1

Related Questions