android-layoutsceneandroid-transitions

How can I change a textView in a layout in a scene before transition?


I follow this guide to make a transition which works, but I have not found out how to change the title1 to title2 programmatically. Android documentation on scene and transition.

I have the following code in my main activity:

Scene startScene, endScene;
ViewGroup sceneRoot = (ViewGroup) rootView.findViewById(R.id.titleRootLayout);
startScene = Scene.getSceneForLayout(sceneRoot, R.layout.scroll_scene_a, this);
endScene = Scene.getSceneForLayout(sceneRoot, R.layout.scroll_scene_end, this);
Transition transition = new AutoTransition();
TransitionManager.go(endScene, transition);

This is the start layout file scroll_scene_a.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/titleTextLayout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/scrollStartImageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/bg_scroll_start"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

This is the ending layout file scroll_scene_end.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/titleTextLayout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/scrollEndImageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/bg_scroll_end"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/storyTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/title1"
        app:layout_constraintBottom_toBottomOf="@id/scrollEndImageView"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@id/scrollEndImageView" />

</androidx.constraintlayout.widget.ConstraintLayout>

And this is the code in where the titleRootLayout is in the main xml file

<androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/titleRootLayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true">

        <include layout="@layout/scroll_scene_a" />

    </androidx.constraintlayout.widget.ConstraintLayout>

This code works fine for the first story, but for the next story I want to change the title1 to title2. I have not found any way to do that. Can someone help me with this?

I tried to change endScene to get hold of the storytitle in many ways, but all ended up with nullpointer exception. In a way this is what I want to do: rootView.findViewById(R.id.storyTitle)).setText(getString(R.string.title2)); But that doesn't work. For one thing the ending layout is defined by the length of the string, so somehow I need to change the string before the endscene is generated. The other thing is how can I actually change the string? I don't want to create a separate xml file for every story. That is bad practice.


Solution

  • I found the solution. I changed the endscene from Scene.getSceneForLayout() to new Scene(sceneRoot, endViewGroup)

    This is my new working main activity

      Scene endScene;
      ViewGroup sceneRoot = (ViewGroup) rootView.findViewById(R.id.titleRootLayout);
      ViewGroup endViewGroup = (ViewGroup) getLayoutInflater().inflate(R.layout.scroll_scene_end, null);
      TextView tv = viewHierarchy.findViewById(R.id.storyTitle);
      tv.setText(getString(R.string.title2));
      endScene = new Scene(sceneRoot, endViewGroup);
      Transition transition = new AutoTransition();