5

Following is my scenario.

I have an Activity MainActivity which has one FAB. When the user clicks on FAB, I open a full screen DialogFragment. I want to open the DialogFragment with some transitions.

Here is the code that I have tried so far.

//MainActivity.java

 final FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
 fab.setOnClickListener(new View.OnClickListener() {
  @Override
  public void onClick(View view) {

    ReviewDialog reviewDialog = ReviewDialog.newInstance();
    Slide slide = new Slide();
    slide.setSlideEdge(Gravity.LEFT);
    slide.setDuration(1000);
    reviewDialog.setEnterTransition(slide);
    Bundle bundle =ActivityOptions.makeSceneTransitionAnimation(ScrollingActivity.this)
        .toBundle();
    reviewDialog.setArguments(bundle);
    reviewDialog.show(getSupportFragmentManager(),"review");
  }
});

And here is the code of the DialogFragment ReviewDialog.

   //ReviewDialog.java
    public class ReviewDialog extends DialogFragment {
  static ReviewDialog newInstance() {
    ReviewDialog f = new ReviewDialog();

    // Supply num input as an argument.
    Bundle args = new Bundle();
    f.setArguments(args);
    return f;
  }

  @Override
  public void onActivityCreated(Bundle arg0) {
    super.onActivityCreated(arg0);
    Slide slide = new Slide();
    slide.setSlideEdge(Gravity.LEFT);
    slide.setDuration(1000);
    getDialog().getWindow().setEnterTransition(slide);
    getDialog().getWindow().setExitTransition(slide);
    getDialog().getWindow().setReenterTransition(slide);}

  @Override
  public void onCreate(Bundle bundle){
    super.onCreate(bundle);
    setStyle(DialogFragment.STYLE_NORMAL,R.style.DialogTheme);
  }

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
                           Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.dialog_review, container, false);
    return v;
  }

}

Also I have set the following property in my AndroidManifest.xml

<item name="android:windowContentTransitions">true</item>

The problem is when the ReviewDialog is started, it doesn't show any transitions. I am able to show transitions between different activities but finding it very hard to show transition between Activity and Fragment. How to show transitions when a DialogFragment is started ?

Manokar
  • 239
  • 3
  • 12
thedarkpassenger
  • 7,158
  • 3
  • 37
  • 61
  • see this http://stackoverflow.com/questions/13402782/show-dialogfragment-with-animation-growing-from-a-point – Harshad Pansuriya May 27 '16 at 11:41
  • @Ironman: the question mentioned in the link was asked way back in 2012 and most of the answers are of 2013. Obviously all answers were written before lollipop transitions were announced. I want to achieve this with the help of the transitions provided in lollipop. – thedarkpassenger May 27 '16 at 12:09
  • see this: http://stackoverflow.com/a/13537234/4628611 – Mehta May 27 '16 at 12:26
  • @Mehta: please read my previous comment. I am trying to achieve this by using the latest transition apis which were introduced in lollipop version. – thedarkpassenger May 27 '16 at 12:30
  • @thedarkpassenger any work around did you find for this problem ? same also happening. I want to transist from activity to dialog fragment in. – Swift Feb 01 '17 at 13:22
  • @thedarkpassenger Did you find solution? – Alexei Korshun Apr 02 '17 at 12:35

1 Answers1

-1

Use this library : https://github.com/lgvalle/Material-Animations

Shared element transition works with Fragments in a very similar way as it does with activities.

a) Enable Window Content Transition

values/styles.xml

<style name="MaterialAnimations" parent="@style/Theme.AppCompat.Light.NoActionBar">
    ...
    <item name="android:windowContentTransitions">true</item>
    ...
</style>

b) Define a common transition name

layout/fragment_a.xml

<ImageView
        android:id="@+id/small_blue_icon"
        style="@style/MaterialAnimations.Icon.Small"
        android:src="@drawable/circle"
        android:transitionName="@string/blue_name" />

layout/fragment_b.xml

<ImageView
        android:id="@+id/big_blue_icon"
        style="@style/MaterialAnimations.Icon.Big"
        android:src="@drawable/circle"
        android:transitionName="@string/blue_name" />

c) Start a fragment with a shared element

FragmentB fragmentB = FragmentB.newInstance(sample);

// Defines enter transition for all fragment views
Slide slideTransition = new Slide(Gravity.RIGHT);
slideTransition.setDuration(1000);
sharedElementFragment2.setEnterTransition(slideTransition);

// Defines enter transition only for shared element
ChangeBounds changeBoundsTransition = TransitionInflater.from(this).inflateTransition(R.transition.change_bounds);
fragmentB.setSharedElementEnterTransition(changeBoundsTransition);

getFragmentManager().beginTransaction()
        .replace(R.id.content, fragmentB)
        .addSharedElement(blueView, getString(R.string.blue_name))
        .commit();

And this is the final result:

enter image description here

Harshad Pansuriya
  • 20,189
  • 8
  • 67
  • 95
  • This thread has a better answer: https://stackoverflow.com/questions/13402782/show-dialogfragment-with-animation-growing-from-a-point – CanonicalBear Nov 19 '20 at 08:13