programmatically adding custom animation to fragment using objectAnimator - android

I currently add custom animation to my fragments using the following code.
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.setCustomAnimations(R.animator.slide_in, R.animator.slide_in, R.animator.slide_out, R.animator.slide_out );
ft.add(R.id.container, new Doze1Fragment());
ft.addToBackStack(null);
ft.commit();
Now I'm playing around with adding a few different fragments on top of each other in a span of 3 to 5 seconds and then pop all of them out of the back stack to get a "fanning" animation out of all fragments. In order to do so I either add a delay in between each popBackStack() call (I think this is a horrible solution), or make a custom XML animator resource for each fragment. I was wondering if anyone has any idea of how to do this programmatically right before calling ft.add(R.id.container, new Doze1Fragment()). I know that normally I could use the following code to animate a view and I was looking for something similar
ObjectAnimator oa = ObjectAnimator.ofFloat(R.id.container, "x", 0, 1000f);
oa.setDuration(300);
oa.start();
Thanks in advance.

Related

Fragment enter and exit transitions are not executed at the same time

Running a simple slide to the left animation for both entering and existing fragment produces the effect of the entering fragment slightly overlapping with the exit fragment. This leads me to think that both transition are not executed at the same time. Any clue or confirmation of this behavior?
The desired effect is to slide the fragments to the left at the same time, without overlap.
The code:
Fragment current = ...;
Fragment fragment = ...;
Transition slideIn = TransitionInflater.from(this)
.inflateTransition(R.transition.fragment_indicator_enter)
.setDuration(300)
.setInterpolator(new LinearInterpolator());
fragment.setEnterTransition(slideIn);
currentFragment.setExitTransition(TransitionInflater.from(this)
.inflateTransition(R.transition.fragment_indicator_exit)
.setDuration(300)
.setInterpolator(new LinearInterpolator()));
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.fragment_container, fragment)
.addToBackStack(null)
.commit();
The only workaround by know it has been to add a setStartDelay(30) for the entering transition. But weird thing, I have different transitions for different fragments and the startDelay has to be different to produce the effect of both fragment sliding to the left at the same time.
The effect is an expected behavior of the transition, as all the views in the layout are moved at a different time to avoid moving everything as a block a create some natural sense of motion. I intentionally want this block effect, so it's solved by adding a target for the transition, where this target is the FrameLayout containing the views of the fragment.
fragment.setEnterTransition(new Slide(Gravity.RIGHT)
.addTarget(R.id.whole_content));
Did you try placing the animations directly in the transaction call?
getSupportFragmentManager()
.setCustomAnimations(R.transition.fragment_indicator_enter, R.transition.fragment_indicator_exit)
.beginTransaction()
.replace(R.id.fragment_container, fragment)
.addToBackStack(null)
.commit();

Android: Hide viewPager tabs when launching a fragment from one of the viewPager tab fragment

I have a viewPager in my app which has several tabs. In some of the tabs, on clicking an item a new fragment is shown. I want this fragment to cover the tabs. Doing this is possible but the approaches don't look good to me.
1.) one way is that I add the newly created fragment to the activity using getSupportFragmentManager(). This solves the problem but doesn't look like a good idea as it will create problems when using back button etc.
2.) Other way is to hide the tabs manually using Visibilty.GONE but problem with this approach is that this hiding of the tabs is visible, I mean the animation could be seen and looks bad.
Is there a better approach to do this problem?
This is my code. "sub_fragment_container" is present in the activity xml, so I get an error java.lang.IllegalArgumentException: No view found for id 0x7f0e00ff (com.my.app:id/sub_fragment_container) for fragment DetailFragment{
FragmentTransaction fragmentTransaction = getChildFragmentManager()
.beginTransaction();
Fragment profileFragment = new DetailFragment();
profileFragment.setArguments(bundle);
fragmentTransaction
.add(R.id.sub_fragment_container, profileFragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
Use android:animateLayoutChanges="true"
Or, to fine tune it further, here's a tested method that I've personally used in my app Newslet to solve the exact same problem.
Wrap your Toolbar and TabLayout within an AppBarLayout. This should be easy if you're aware of the Design Support Library. If not, this should get you started.
Add this piece of code in your onCreate():
LayoutTransition layoutTransition = new LayoutTransition();
layoutTransition.setDuration(200);
layoutTransition.setStartDelay(LayoutTransition.CHANGE_APPEARING, 0);
layoutTransition.setStartDelay(LayoutTransition.APPEARING, 0);
layoutTransition.setStartDelay(LayoutTransition.DISAPPEARING, 0);
layoutTransition.setStartDelay(LayoutTransition.CHANGE_DISAPPEARING, 0);
appBarLayout.setLayoutTransition(layoutTransition);
After this, when you remove the Tabs or add it back, the layout change will animate smoothly.
Hope this helped you!

How to switch Tabs without Animating Fragments?

I have an application with a stack of Fragments added on top of each other.
The Fragments are animated to slide in when opened, and slide out when popped from the backstack. Here's a sample of where I open each Fragment:
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.setCustomAnimations(R.anim.slide_in_right,0,0,android.R.anim.slide_out_right);
ft.add(R.id.examplecontainer, examplefragment);
ft.addToBackStack(null);
ft.commitAllowingStateLoss();
The animations work as expected, and everything is good.. except that my App also has a couple more Tabs managed by a FragmentTabHost.
When I switch to another Tab and come back, I want the top Fragment to appear without any animations. Instead, it slides in again.
Is there any way to disable animations when switching between tabs on FragmentTabHost? Or any way to remove/change the custom animations set to a fragment?
Thank you in advance for any hints/suggestions.

Activity with Multiple Animated Fragments

I currently have one Activity which contains a Fragment by injecting it inside a LinearLayout during runtime.
Inside my Activity layout, I have a Button view called nextButton. When I click this button, I want the Activity to switch to the next Fragment but ALSO animate where the current fragment moves off the screen to the left and the new fragment comes in from the right. As if the new fragment is pushing the current fragment out of the way.
Below is a demonstration of what I want.
Shall I store all of the Fragments in an ArrayList<Fragment> and just inject the current index + 1 in to the LinearLayout when the nextButton is clicked? What would be the best way to go about this?
All you need is ViewPager, See one good example here
You need to do selectPage with in your nextButton, here true is for smooth scroll animation.
pager.setCurrentItem( num,true )
Please find Reference link here
You need to create 4 animation for entering (left and right) and exiting animation (left and right) of the fragment.
Each time you replace a fragment from the FragmentTransaction you need to set the setCustomAnimations
sample:
FragmentTransaction transaction = activity.getSupportFragmentManager().beginTransaction();
transaction.setCustomAnimations(inL, inR, outL, outR);
transaction.replace(layout, fragment, tag);
transaction.commit();
where inL and inR are entering animation and outL and outR are exiting animation animation

Android - Animate fragment entrance after it's drawn?

I'm using the Support Library and replacing a fragment with a custom slide-in animation.
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.setCustomAnimations(enterIn, enterFragmentOut);
transaction.replace(R.id.content_frame, contentFragment);
transaction.commit();
The problem I'm experiencing is that the animation starts concurrently with the drawing of the fragment's views, and since the fragment has a rather complex layout (ListViews, &c) the animation is very stuttery (only a handful of frames are shown).
I've tried splitting the operation into two parts (with transaction1 = {add_new, hide}, transaction2 = {remove_old, show}) and firing the second transaction after onCreateView() has executed on the new fragment, but I didn't get much improvement at all.
What I'm looking for is a way to start the animation after the new fragment is fully drawn, if that's at all possible. Or any other tips that you may have on this issue...
Any ideas? Thanks a lot.

Categories

Resources