Is it possible to programmatically set the xml property transitionName to a Toolbar title and then use it for an animation using ActivityOptionsCompat?
This is how I declare my Toolbar:
Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);
myToolbar.setTitle("title");
And this is my xml declaration:
<android.support.v7.widget.Toolbar
android:id="@+id/my_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/primary_color"
android:elevation="4dp"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
android:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
In order to add the animation I have to use the following code:
ActivityOptionsCompat options =
ActivityOptionsCompat.makeSceneTransitionAnimation(activity,
albumCoverImageView, // The view which starts the transition (Should be myToolbar title)
transitionName // The transitionName of the view
);
ActivityCompat.startActivity(activity, intent, options.toBundle());
How can I pass the Toolbar (TextView ?) title to the makeSceneTransitionAnimation function?
You can get the TextView instance of the Toolbar's title and set transition by following this code:
private Toolbar mToolbar;
private TextView mTextViewToolbarTitle;
@Override
protected void onCreated(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.your_layout);
mToolbar = (Toolbar) findViewById(R.id.your_toolbar_id);
setSupportActionBar(mToolbar);
mTextViewToolbarTitle = getTextViewTitle(mToolbar);
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
mTextViewTitle.setTransitionName(getString(R.string.your_transition_name));
}
}
Copy this method in your activity:
public static TextView getTextViewTitle(Toolbar toolbar){
TextView textViewTitle = null;
for(int i = 0; i<toolbar.getChildCount(); i++) {
View view = toolbar.getChildAt(i);
if(view instanceof TextView) {
textViewTitle = (TextView) view;
break;
}
}
return textViewTitle;
}
then start your new activity with transition by:
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
startActivity(intent,
ActivityOptionsCompat.makeSceneTransitionAnimation(this,
mTextViewToolbarTitle,
getString(R.string.your_transition_name))
.toBundle());
} else {
startActivity(intent,
ActivityOptionsCompat.makeSceneTransitionAnimation(this).toBundle());
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With