androidandroid-activitymaterial-designandroid-transitionsshared-element-transition

Required Transition found: MaterialContainerTransform?


In my activity, I'm trying to use MaterialContainerTransform but it shows an error that

Required Transition found: MaterialContainerTransform

override fun onCreate(savedInstanceState: Bundle?) {
window.requestFeature(Window.FEATURE_ACTIVITY_TRANSITIONS)
    setEnterSharedElementCallback(MaterialContainerTransformSharedElementCallback())
    //this is giving error
    window.sharedElementEnterTransition = MaterialContainerTransform().apply {
        addTarget(android.R.id.content)
        duration = 300L
    }
    window.sharedElementReturnTransition = MaterialContainerTransform().apply {
        addTarget(android.R.id.content)
        duration = 250L
    }

My dependencies

dependencies {
implementation fileTree(dir: "libs", include: ["*.jar"])
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.core:core-ktx:1.3.1'
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.0-beta3'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
testImplementation 'junit:junit:4.13'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
//material
implementation 'com.google.android.material:material:1.3.0-alpha02'

def coroutines_version = "1.3.8"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutines_version"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutines_version"
implementation 'androidx.navigation:navigation-fragment-ktx:2.3.0'
implementation 'androidx.navigation:navigation-ui-ktx:2.3.0'
implementation 'androidx.viewpager2:viewpager2:1.0.0'

}


Solution

  • There are two types of MaterialContainerTransform the com.google.android.material.transition.MaterialContainerTransform that uses AndroidX Transition and includes bug fixes that apply to all API levels and the com.google.android.material.transition.platform.MaterialContainerTransform class that is built on the framework Transition class.

    Only the framework Transition version can be used for window transitions, so you should make sure you're importing the right version of MaterialContainerTransform:

    import com.google.android.material.transition.platform.MaterialContainerTransform