Skip to content Skip to sidebar Skip to footer

Slide In Another Xml Layout When Button Is Clicked

I have a main.xml layout file and in my Java code, I am doing some calculation taken from the main layout and wanting to display in result.xml layout file. I know I can use the ani

Solution 1:

You can also use this function : overridePendingTransition which can define an animation for the upcoming activity and an other animation for the activity which exit.

Use it like this, in your second Activity :

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    overridePendingTransition(android.R.anim.slide_out_right, 0);
    setContentView(R.layout.myLayout);

first argument is for the animation enter, and 0 is for no animation.

And for an animation when you leave your second activity :

@Override
public void finish() {
    super.finish();
    overridePendingTransition(R.anim.enterAnim, R.anim.leaveAnim);
}

Solution 2:

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.

Post a Comment for "Slide In Another Xml Layout When Button Is Clicked"