Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set transitionName to Toolbar title?

I am trying to add an animation with "makeSceneTransitionAnimation" in a Toolbar title, but like it is private, i can't to set a "transitionName" on it.

If Anybody know how resolve this or know other way help me.

like image 257
Busata Avatar asked Jul 08 '15 13:07

Busata


1 Answers

Custom title TextView

Because you're using Toolbar you can consider the possibility of directly creating a custom title TextView:

Disable the default ActionBar title:

@Override
public void onCreate(Bundle savedInstanceState) {
    // ...
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    // Either by calling
    getSupportActionBar().setTitle(null);
    // Or
    toolbar.setTitle(null);
    // Or
    getSupportActionBar().setDisplayShowTitleEnabled(false);
    // ...
}

there are lots of other ways of disabling the default title from XML as well, some are discussed in this question: Remove title in Toolbar in appcompat-v7.

Then in Activity's layout file add a custom title TextView inside Toolbar:

<android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:theme="@style/AppTheme"
        app:popupTheme="@style/AppTheme.PopupOverlay">

     <TextView
            android:id="@+id/custom_title"
            android:text="@string/activity_title"
            android:textAppearance="@style/TextAppearance.AppCompat.Widget.ActionBar.Title"
            android:transitionName="@string/title_transition_name"/>

 </android.support.v7.widget.Toolbar>

textAppearance can be used to apply the same style of the default ActionBar title.

This approach allows you to have more control over the title view, especially if you need to play some additional TextView properties Animations together with your sceneTransition and you want to be able to explicitly define the final appearance of the title.

Find default title TextView

If you prefer, the default title TextView can be found in different ways:

@Override
public void onCreate(Bundle savedInstanceState) {
    // ...
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    // This assumes that the title is the first child of Toolbar
    TextView titleTextView = (TextView) toolbar.getChildAt(0);
    // Or this assumes that the title is the first TextView inside of Toolbar
    for(int i = 0; i < toolbar.getChildCount(); ++i) {
        View child = toolbar.getChildAt(i);
        if(view instanceof TextView) {
            titleTextView = (TextView) view;
            break;
        }
    }
}

the transitionName can then be set:

titleTextView.setTransitionName(/* Transition name */);

more ways to find title TextView are discussed here Getting ActionBar Title TextView with AppCompat v7 r21.

like image 176
Umbo Avatar answered Oct 20 '22 12:10

Umbo