Android fragment `setEnterTransition` doesn't work - android

I'm trying to animate the showing of a fragment in a very basic way but it doesn't work:
MyFragment myFragment = (MyFragment) getFragmentManager().findFragmentById(R.id.myFragment);
getFragmentManager().beginTransaction()
.hide(myFragment)
.commit();
Slide slideRight = new Slide(Gravity.RIGHT);
slideRight.setDuration(3000);
Slide slideLeft = new Slide(Gravity.LEFT);
slideLeft.setDuration(3000);
myFragment.setEnterTransition(slideRight);
myFragment.setExitTransition(slideLeft);
then later
getFragmentManager().beginTransaction()
.show(myFragment)
.commit();
But fragment just appears without any transition. I can't figure out why it doesn't animate.
Edit:
I ended up using the fragment support library with setCustomAnimations cause the native library doesn't support animation with relative values.
Also using the new API with setEnterTransition doesn't seem to work properly with show/hide and for some unknown reason worked only with replace which doesn't fit my needs.

setCustomAnimation will do the trick.
FragmentManager.beginTransaction()
.replace(R.id.fragment, new Fragment)
.setCustomAnimations(R.anim.slide_out_left, R.anim.slide_in_right).commit();

Related

How to display loading animation when transitioning between fragmens?

I am still in fragment, for display new fragment using the code:
CatalogFragment catalogFragment = new CatalogFragment();
var fragmentManager = Activity.SupportFragmentManager.BeginTransaction();
fragmentManager.Replace(Resource.Id.flContent, (SupportFragment)catalogFragment);
fragmentManager.AddToBackStack(null);
fragmentManager.Commit();
How to display loading animation (such as "animated circle" or gif) when transitioning between fragmens?
Sorry for my English.
For animated circle you should put a progress dialog before replacing the fragment. After that you should disable it in new fragment. But fragments can be animated natively. You should checkout this link. https://developer.android.com/training/animation/cardflip.html

How can I use chenupt/SpringIndicator in Fragment

I want to use it in a v4.Fragment to load childFragment, and the SpringIndicator sample code based on MultipleModel.
I tryed to deliver a ChildFragmentManager to ModelPagerAdapter in Fragment follow this:
ModelPagerAdapter adapter = new ModelPagerAdapter(getChildFragmentManager(), new PagerModelManager());
But that not works, nothing displayed on phone's screen.
Actually,The SpringIndicator will works in a Activity.(use getSupportFragmentManager as the arg)
How can I do?Thank you very much.
Sorry,everyBody.That code in question taked effect but not display beacause of background color.So, You can use SpringIndicator in Fragment to load childFragment with:
ModelPagerAdapter adapter = new ModelPagerAdapter(getChildFragmentManager(), new PagerModelManager());
The first arg is getChildFragmentManager() worked for me.

Flip animation (ObjectAnimator) for supportfragment (API less than 10)

I tried use CustomAnimation function for my application. But I end up getting Unknown Object Animator name error. I did some research and found out apparently objectAnimator type of animation is not supported under getSupportFragmentManager (correct me if i'm wrong).
getSupportFragmentManager()
.beginTransaction()
.setCustomAnimations(R.animator.card_flip_left_in, R.animator.card_flip_left_out,
R.animator.card_flip_right_in, R.animator.card_flip_right_out)
.add(R.id.content_frame, reportPage,reportPage.getClass().getSimpleName()).addToBackStack(reportPage.getClass().getSimpleName())
.commit();
getSupportFragmentManager().executePendingTransactions();
I saw multiple threads mentioned use http://nineoldandroids.com/ for ObjectAnimator. But I don't have an idea of how to use this library to implement it on my application.
Is there any workaround that I can use Object Animator type of animation in the above function?

How to use default animations in Android?

I'm trying to use the default animations for the Activity with fragments.. here I found something about it:
Android: using Activity's default animation for Fragments
the problem is: ok, I need (for example) "activityOpenEnterAnimation".. how can I use it?
Using the following code won't work:
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.setCustomAnimations(android.R.anim.activityOpenEnterAnimation, android.R.anim.activityOpenExitAnimation);
transaction.replace(R.id.container, fragment)
.addToBackStack(((Object) fragment).getClass().getName())
.commit();
Hints? Thanks! :)
Nowadays, Android documentation clearly recommends not to use resources directly from android.R.*, since every release of the platform has changes on them. Even some resources dissapear from one version to another, so you shouldn't rely on them. On the other hand, lots of resources are private and not available from a developer's code.
The safest (and recommended) way is to simply copy & paste the resources you need (in this case, the animations) from the source code of the Android version you want into your own code, and use them through the regular R.*.
You can browse the Android source code in many ways, as explained in [1].
[1] Where can I find Android source code online?
example of using default android animation when back pressed happen
#Override
public void onBackPressed() {
super.onBackPressed();
overridePendingTransition(android.R.anim.fade_in,android.R.anim.fade_out);
}
for using default animations you should use android.R.anim.ANIMATIONNAME
I managed to get it to work this way:
static public int getResourceIdFromCurrentThemeAttribute(FragmentActivity activity, int attribute){
TypedValue a = new TypedValue();
activity.getTheme().resolveAttribute(attribute, a, false);
return a.resourceId;
}
//This type of fragment will be opened like an activity
static public void openActivityLikeFragment(FragmentActivity activity, BaseFragment fragment, int containerId, String stackRef) {
FragmentManager fm = activity.getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
//The fragment open/close transition should have the same animations as its activity
ft.setCustomAnimations(
getResourceIdFromCurrentThemeAttribute(activity, android.R.attr.activityOpenEnterAnimation),
getResourceIdFromCurrentThemeAttribute(activity, android.R.attr.activityOpenExitAnimation),
getResourceIdFromCurrentThemeAttribute(activity, android.R.attr.activityCloseEnterAnimation),
getResourceIdFromCurrentThemeAttribute(activity, android.R.attr.activityCloseExitAnimation)
);
ft.replace(containerId, fragment);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.addToBackStack(stackRef);
ft.commit();
}
This solution is safer than directly referencing a resource, since its referencing an attribute which won't change without some deprecation warnings first.

Create fragments, for older versions of android

How can I use fragments, in older versions of android.
For android HoneyComb and early I write this code:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
FragmentManager fragmentManager = getFragmentManager();
Fragment menuFragment = new MenuFragment();
fragmentManager.beginTransaction().replace(R.id.topMenu, menuFragment).commit();
Fragment contentFragment = new AlleFragment();
fragmentManager.beginTransaction().replace(R.id.container, contentFragment).commit();
}
But I don't know how to write this code to run for older version of android.
Can someone to help me?
You can include support library v4 which comes with Android SDK. You have example in official Android documentation: http://developer.android.com/training/basics/fragments/creating.html

Categories

Resources