package com.alexstyl.searchtransition.mainscreen; import android.content.Intent; import android.os.Bundle; import android.transition.Transition; import android.transition.Transition.TransitionListener; import android.transition.TransitionManager; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.FrameLayout; import com.alexstyl.searchtransition.boilerplate.BoilerplateActivity; import com.alexstyl.searchtransition.R; import com.alexstyl.searchtransition.searchscreen.SearchActivity; import com.alexstyl.searchtransition.transition.FadeInTransition; import com.alexstyl.searchtransition.transition.FadeOutTransition; import com.alexstyl.searchtransition.transition.SimpleTransitionListener; public class MainActivity extends BoilerplateActivity { private SimpleToolbar toolbar; private int toolbarMargin; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // setup your Toolbar as you would normally would // For simplicity, I am wrapping the styling of it into its a separate class toolbar = (SimpleToolbar) findViewById(R.id.main_toolbar); setSupportActionBar(toolbar); toolbarMargin = getResources().getDimensionPixelSize(R.dimen.toolbarMargin); toolbar.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // Prepare the keyboard as soon as the user touches the Toolbar // This will make the transition look faster showKeyboard(); transitionToSearch(); } }); } private void transitionToSearch() { // create a transition that navigates to search when complete Transition transition = FadeOutTransition.withAction(navigateToSearchWhenDone()); // let the TransitionManager do the heavy work for us! // all we have to do is change the attributes of the toolbar and the TransitionManager animates the changes // in this case I am removing the bounds of the toolbar (to hide the blue padding on the screen) and // I am hiding the contents of the Toolbar (Navigation icon, Title and Option Items) TransitionManager.beginDelayedTransition(toolbar, transition); FrameLayout.LayoutParams frameLP = (FrameLayout.LayoutParams) toolbar.getLayoutParams(); frameLP.setMargins(0, 0, 0, 0); toolbar.setLayoutParams(frameLP); toolbar.hideContent(); } private TransitionListener navigateToSearchWhenDone() { return new SimpleTransitionListener() { @Override public void onTransitionEnd(Transition transition) { Intent intent = new Intent(MainActivity.this, SearchActivity.class); startActivity(intent); // we are handing the enter transitions ourselves // this line overrides that overridePendingTransition(0, 0); // by this point of execution we have animated the 'expansion' of the Toolbar and hidden its contents. // We are half way there. Continue to the SearchActivity to finish the animation } }; } @Override protected void onResume() { super.onResume(); // when you are back from the SearchActivity animate the 'shrinking' of the Toolbar and // fade its contents back in fadeToolbarIn(); // in case we are not coming here from the SearchActivity the Toolbar would have been already visible // so the above method has no effect } private void fadeToolbarIn() { TransitionManager.beginDelayedTransition(toolbar, FadeInTransition.createTransition()); FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) toolbar.getLayoutParams(); layoutParams.setMargins(toolbarMargin, toolbarMargin, toolbarMargin, toolbarMargin); toolbar.showContent(); toolbar.setLayoutParams(layoutParams); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { if (item.getItemId() == R.id.main_action_share) { shareDemo(); return true; } return super.onOptionsItemSelected(item); } }