Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is it possible to do transition animations when changing views in the same activity?

Tags:

android

Suppose I have 2 XML files and my activity will setContentView the appropriate one based on some button press from the user. Is it possible to change the transition animation for the changing of content view?

So far I see super.overridePendingTransition() which is suitable for starting new activities, however my example does not start a new activity, it just changes the layout in the current one.

like image 223
damonkashu Avatar asked Dec 15 '10 02:12

damonkashu


People also ask

What is transition animation in Android?

Android's transition framework allows you to animate all kinds of motion in your UI by simply providing the starting layout and the ending layout.

How do I change an animation layout?

All you need to do is set an attribute in the layout to tell the Android system to animate these layout changes, and system-default animations are carried out for you. Tip: If you want to supply custom layout animations, create a LayoutTransition object and supply it to the layout with the setLayoutTransition() method.


2 Answers

Mathias Lin has explained it very well.

You can always use default stock animations supplied by Android framework.

Heres an example code:

boolean isFirstXml=evaluatingConditionFunction();
LayoutInflater inflator=getLayoutInflater();
View view=inflator.inflate(isFirstXml?R.layout.myfirstxml:R.layout.myseconxml, null, false);
view.startAnimation(AnimationUtils.loadAnimation(this, android.R.anim.slide_out_right));
setContentView(view);

Call this from any of your activity which holds your Parent View.

For custom animations you can visit developer docs. Heres the documentation link.

like image 140
Shardul Avatar answered Sep 25 '22 14:09

Shardul


Yes, you can apply an animation on almost any view you like. Just via view.startAnimation(animation);

Take the outer viewgroup of your respective layout (content view) and apply the animation to it. Depending what kind of animation you want to do, it might make sense to inflate/load both layouts but hide one of them and then swap. Please specify what kind of transition you have in mind.

For example: if you do an alpha transition, you would run the alphaAnimation on the current layout, when when the animation ends (AnimationListener), you set the content view to the new layout, and fade the content back in, via another alphaAnimation.

like image 33
Mathias Conradt Avatar answered Sep 21 '22 14:09

Mathias Conradt