Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Varargs Kotlin Java interop not working properly

For the makeSceneTransitionAnimation there are two static functions

public static ActivityOptionsCompat makeSceneTransitionAnimation(Activity activity,
        View sharedElement, String sharedElementName)

and

    public static ActivityOptionsCompat makeSceneTransitionAnimation(Activity activity,
        Pair<View, String>... sharedElements)

The first function call works properly in Kotlin but when calling the second one, both these calls return errors

        val imageTransition = Pair<View, String>(imageView, imageView.getTransitionName());
        val textTransition = Pair<View, String>(textView, textView.getTransitionName());
        val transitionList = Array(2, { imageTransition });
        transitionList[1] = textTransition;
        val options = ActivityOptionsCompat.makeSceneTransitionAnimation(this, *transitionList);

and

        val imageTransition = Pair<View, String>(imageView, imageView.getTransitionName());
        val textTransition = Pair<View, String>(textView, textView.getTransitionName());
        val options = ActivityOptionsCompat.makeSceneTransitionAnimation(this, imageTransition, textTransition);

Is there a proper way to get this working or is this an issue with the interop?

Edit Added change to ensure that it is using the same classes

val imageView : View = view.findViewById(android.R.id.icon);
val textView : View = view.findViewById(android.R.id.text1);
imageView.setTransitionName("imageTransition");
textView.setTransitionName("textTransition")

val imageTransition : android.support.v4.util.Pair<android.view.View, java.lang.String> = android.support.v4.util.Pair.create(imageView, imageView.getTransitionName() as java.lang.String);
val textTransition : android.support.v4.util.Pair<android.view.View, java.lang.String> = android.support.v4.util.Pair.create(textView, textView.getTransitionName() as java.lang.String);
val transitionList = Array(2, { imageTransition });
transitionList[1] = textTransition;
val options = ActivityOptionsCompat.makeSceneTransitionAnimation(this, *transitionList);

Current compiler error:

Error:(72, 84) The spread operator (*foo) may only be applied in a vararg position
Error:(72, 99) No value passed for parameter sharedElementName

And another

val imageView : View = view.findViewById(android.R.id.icon);
val textView : View = view.findViewById(android.R.id.text1);
imageView.setTransitionName("imageTransition");
textView.setTransitionName("textTransition")

val imageTransition : android.support.v4.util.Pair<android.view.View, java.lang.String> = android.support.v4.util.Pair.create(imageView, imageView.getTransitionName() as java.lang.String);
val textTransition : android.support.v4.util.Pair<android.view.View, java.lang.String> = android.support.v4.util.Pair.create(textView, textView.getTransitionName() as java.lang.String);
val options = ActivityOptionsCompat.makeSceneTransitionAnimation(this, imageTransition, textTransition);

Current compiler error:

Error:(72, 84) Type mismatch: inferred type is android.support.v4.util.Pair<android.view.View, java.lang.String> but android.view.View! was expected
Error:(72, 101) Type mismatch: inferred type is android.support.v4.util.Pair<android.view.View, java.lang.String> but kotlin.String! was expected
like image 989
Josh Feinberg Avatar asked Sep 16 '15 06:09

Josh Feinberg


People also ask

Are Java libraries compatible with Kotlin?

Yes, as Kotlin is 100% interoperable with Java, and both works on JVM. so one can easily use Java libraries with Kotlin.

Can you use Java and Kotlin together?

Android Studio provides full support for Kotlin, enabling you to add Kotlin files to your existing project and convert Java language code to Kotlin. You can then use all of Android Studio's existing tools with your Kotlin code, including autocomplete, lint checking, refactoring, debugging, and more.

Is all Java valid Kotlin?

Is Kotlin compatible with the Java programming language?  Yes. Kotlin is 100% interoperable with the Java programming language and major emphasis has been placed on making sure that your existing codebase can interact properly with Kotlin.

How can I call Kotlin from Java?

Kotlin code access Java array We can simply call Java class method which takes array as an argument from Kotlin file. For example, create method sumValue() which takes array element as parameter in Java class MyJava.


1 Answers

This worked for me:

import android.support.v4.util.Pair

// ...    

val options = ActivityOptionsCompat.makeSceneTransitionAnimation(activity,
        Pair<View, String>(image, image.transitionName),
        Pair<View, String>(title, title.transitionName))

startActivity(intent, options.toBundle())
like image 76
Gnzlt Avatar answered Sep 21 '22 18:09

Gnzlt