I have a scenario in which from an activity, on the click of a button, I need to show a fragment with sliding in from right to left and similarly when I press back fragment slides back to the right and shows my previous activity from which the fragment was added.
Now my issue is that the animation to the fragment itself works but I need to animate the activity by sliding out to the left while the fragment is sliding in from the right.
Tried using overridePendingTransition method to do this:
overridePendingTransition(R.anim.push_in_from_left, R.anim.push_out_to_left);
I tried putting the pending transition in the methods available with the activity onResume, onPause, etc. but didn't get it to work. Following is the code I am using:
overridePendingTransition(R.anim.push_in_from_left, R.anim.push_out_to_left);
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.setCustomAnimations(R.anim.pull_in_from_left, R.anim.push_out_to_left, R.anim.pull_in_from_left, R.anim.push_out_to_left);
transaction.add(R.id.events_holder, myFragment);
transaction.addToBackStack("myFragment");
transaction.commit();
Tried to put the pending transition on the click of the button as well, before I start doing the fragment transaction things, but didn't work either.
It would be really helpful if anybody can suggest a solution to this problem.
You aren't overriding any animation, sliding in a fragment should not move the layout of the activity if you are doing it right. Instead you have to apply an animation which you can define in an xml to the layout containing your activity. In essence you will give the appearance your activity slides out, and fragment slides in. Depending on what target you are building for, after you apply the animation you may need to actually move (in this case applying an animation doesnt actually change the physical location of the layout, it will simply render where you tell it.) the layout containing the fragment. You dont need to physically move the layout containing the activity, because if you specify an empty onclick listener for the bottom layer layout in the fragment no clicks will fall through. When you are done, use another xml animation file to slide the fragment and Activity layout back the way they were (so an opposite animation) and then physically move the fragment back out of the way, or it will still receive all of the on click events. (I do believe this issue has been fixed if you are targeting a new sdk)
an animation xml looks something like this
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="100%p" android:toXDelta="0" android:duration="300"/>
</set>
Related
I want to create an animation between two fragments on Android.
After clicking on the "menu" button, I want to scale[minimize] current fragment and move it to right corner of the second fragment like this:
Preview
And after click on this minimized fragment, I want to maximize it again with reversed animation.
Is it generally possible? How can I do this?
There are several libraries available for viewpager animations like
https://github.com/jfeinstein10/JazzyViewPager
https://github.com/ToxicBakery/ViewPagerTransforms
I've chosen to hide/show my fragments during navigation for smoother animations.
To display a fragment, I add an animation :
transaction.setCustomAnimations(R.anim.from_right,
R.anim.to_left, R.anim.from_left, R.anim.to_right);
Then I simply hide previous one, show next one.
The problem is if I want to "popback" the fragment, I don't use the normal addToBackStack(null);
Instead I just hide the previous one and show the next one again. But this way the animation is not played. I don't want to save which fragment should use which animation every time.
How do I use the animations I initially set up in setCustomAnimations when I leave the fragment, in the same way a popBackStack would do it?
I am try to do two animation while navigation from one fragment to other fragment.
When I am in onefragment,On the tab click of activity,current fragment should slide left and on the launch of new fragment it should be sliding from right.
I tried intially using FragmentTransaction's setCustomAnimations(),but it was not successful.
So I am doing animations on status of fragment.So the SlideLeft transititionm,I am doing in Stop() of the fragment,While SlideIn transistion has been done on OnAcitvityCreated() of the fragment.
Please let me know Is this correct way or any other appropriate way to handle this
I'm trying to animate 2 simple Views from a selected item in a RecyclerView to a new fragment. I've looked at a lot of examples of animating shared elements from one Activity to another Activity, but very few examples of animating a shared element from one Fragment to another Fragment within the same Activity. It almost works.
Here is my structure.
Activity
-- Full screen Fragment1 with RecyclerView
-- Full screen Fragment2 with details
When the user selects an item in the RecyclerView in Fragment1, I replace Fragment1 with Fragment2 that has a View with the shared elements in it in different positions and sizes.
There's a bit of a trick to get it to work, you have to make sure your transitionName is unique for each item in your list, and of course that transitionName must match the transitionName of the element in Fragment2 for the animation to play. I have this part working, when I select an item, the 2 shared Views do animate, just not exactly how you would expect when doing it between 2 Activities.
If I select an item near the bottom of the screen, it draws the View for Fragment2 and animates the 2 shared Views as if they were in the item at the top of the screen. Hard to explain. Here are some pictures
Fragment1
Fragment2
In both fragments I'm setting the following
setSharedElementEnterTransition(new ChangeBounds());
setSharedElementReturnTransition(new ChangeBounds());
setAllowEnterTransitionOverlap(true);
setAllowReturnTransitionOverlap(true);
Also in their parent Activity in onCreate() I've set
getWindow().requestFeature(Window.FEATURE_CONTENT_TRANSITIONS);
Any idea why my shared element animations are starting at the top of my screen even when the they were starting in the selected item at the bottom of my screen?
Finally solved this problem! As it turns out because the view I'm sharing between 2 fragments is a child of another view (RelativeLayout) in the 2nd fragment, you need to add the ChangeTransform transition to your TransitionSet. Apparently ChangeTransform tells the system to remember the views original position in the 1st fragment before animating to the new position in the 2nd fragment. Here is my updated transitionSet. I'll also clean up my test project code a bit and make a final push to bitbucket in case it will help others after me. Thanks for all the help with this one Alex and thank you to #George-mount for answering someones similar question that dropped the hint to me for this solution.
<?xml version="1.0" encoding="utf-8"?>
<transitionSet xmlns:android="http://schemas.android.com/apk/res/android">
<changeTransform/>
<changeBounds/>
</transitionSet>
https://bitbucket.org/brockoli/fragmentsharedelements
in my Activity, I have a layout containing 3 FrameLayouts, one at the top, one at the left and one at the "center".
Now, I sometimes only want to display one or two of them. Atm I am doing it this way:
FrameLayout frame = (FrameLayout) findViewById(R.id.framelayout_menu_left);
frame.setVisibility(...);
frame = (FrameLayout) findViewById(R.id.framelayout_content);
frame.setVisibility(...);
frame = (FrameLayout) findViewById(R.id.framelayout_menu_top);
frame.setVisibility(...);
However this can get really ugly results, e.g. when I switch the "content" Fragment and hide the top and/or left FrameLayout. It all starts flickering as the "content" Fragment jumps to the top and/or left and only afterwards is replaced.
Also, I can obviously not navigate back to another setup, so is there any other way to do this?
Kind regards,
jellyfish
Edit:
Maybe a little drawing makes my question clearer...
A shows a Layout of 3 FrameLayouts containing 3 different Fragments. Each color represents one distinct Fragment.
Now what I want to do is to switch from A to D.
I am doing this by replacing the blue Fragment with the yellow Fragment via a FragmentTransaction.
However, this still keeps the other Frames visible, so I hide them via the code above.
Now, Frame.setVisibility() is called way before commit(), so in B and C the blue Fragment "jumps" to the left and the top and only afterwards (in D) is replaced with the yellow Fragment. This produces a nasty flickering.
As a workaround, I now hide all three FrameLayouts before the transaction and re-show the ones I need once the transaction has finished. But there still is the problem that I can't go back via the back button as this isn't a real transaction.
I would have two suggestions. Firstly, if you both add a fragment transition effect and do the visibility changes after the transaction, that would probably substantially reduce much of your flicker effect
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
Secondly, I've simply given up on having the system manage the fragment stack for me -- it seems that this only works well with simple transactions. Override onBackPressed and do your own logic there.
--randy