18

In Java, instantiate an interface object is as easy as new Interface()... and override all the required functions as below, on AnimationListener

private void doingSomething(Context context) {
    Animation animation = AnimationUtils.loadAnimation(context, android.R.anim.fade_in);
    animation.setAnimationListener(new Animation.AnimationListener() {
        // All the other override functions
    });
}

However, in Kotlin when we type

private fun doingSomething(context: Context) {
    val animation = AnimationUtils.loadAnimation(context, android.R.anim.fade_in)
    animation.setAnimationListener(Animation.AnimationListener(){
        // All the other override functions
    })
}

It error complaints unresolved References AnimationListener.

Malt
  • 28,965
  • 9
  • 65
  • 105
Elye
  • 53,639
  • 54
  • 212
  • 474
  • 1
    Possible duplicate of [How to create an instance of anonymous interface in Kotlin?](http://stackoverflow.com/questions/37672023/how-to-create-an-instance-of-anonymous-interface-in-kotlin) – miensol Jun 14 '16 at 08:16

2 Answers2

47

As explained in the documentation:

animation.setAnimationListener(object : Animation.AnimationListener {
    // All the other override functions
})
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • Thanks! Great. When I search in https://kotlinlang.org/docs/reference/interfaces.html, it didn't even touch anything on that. The documentation is quite obscured. Search on the internet also can't find it. The only place to find this is to manually write a Java code, and convert it... Hopefully my stackoverflow question would help others searching for this quickly to find it. Thanks! – Elye Jun 14 '16 at 07:48
  • I suggested to improve the documentation in the kotlin slack. I agree that the information isn't that easy to find in the doc. – JB Nizet Jun 14 '16 at 07:57
  • That sounds great! – Elye Jun 14 '16 at 08:01
  • 2
    The documentation has been improved: https://kotlinlang.org/docs/reference/nested-classes.html#anonymous-inner-classes and https://kotlinlang.org/docs/reference/classes.html#creating-instances-of-classes – JB Nizet Jun 14 '16 at 10:28
  • Cool for improving the documentation. Look at the number of 'tick' at your answer within two days. Clearly this is a hot question and your answer helps to answer them. – Elye Jun 15 '16 at 11:53
  • I dont know why you rollback my edit but can you tell IntelliJ to fix this: https://ibb.co/mHZuqm – Ultimo_m Oct 20 '17 at 12:07
  • 1
    @Ultimo_m sorry for that. The syntax with parentheses is correct to create an anonymous object extending a class and calling its default constructor. When implementing an interface, the parentheses should indeed not be there. – JB Nizet Oct 20 '17 at 16:03
6

Apparently the latest way (using Kotlin 1.0.5) of doing it is now without the parenthesis, given there's no empty constructor for the interface.

animation.setAnimationListener(object : Animation.AnimationListener {
    // All the other override functions
})
Elye
  • 53,639
  • 54
  • 212
  • 474