How to put set of animations on 2 buttons through xml?












0















I'm playing with animating my buttons, and when I click a button that button is supposed to move along the y-axis and fade out while another button will fade in on the y-axis. I've achieved this by writing the code in my java class and it works perfectly fine, but I want to shift all that code to my XML and call from there. these animations. How can I make a set of animations this way i.e Translate, fade out and another translate and fade in?



 public static void crossfade(Button buttonToFadeOut, Button buttonToFadeIn) {

buttonToFadeIn.setAlpha(0f);
buttonToFadeIn.setVisibility(View.VISIBLE);

buttonToFadeIn.animate()
.alpha(1f)
.translationY(100) //220
.setDuration(700)
.setStartDelay(60)
.setListener(null);

buttonToFadeOut.animate()
.alpha(0f)
.setDuration(700)
.translationY(-100)
.setListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
buttonToFadeOut.setVisibility(View.GONE);
}
});
}









share|improve this question

























  • Hello. Was my answer able to help you?

    – Ishaan Javali
    Nov 23 '18 at 3:10











  • @Ishaan .. Yes sorry i had to go out, replied late.

    – Muhammad Abdullah
    Nov 23 '18 at 6:49
















0















I'm playing with animating my buttons, and when I click a button that button is supposed to move along the y-axis and fade out while another button will fade in on the y-axis. I've achieved this by writing the code in my java class and it works perfectly fine, but I want to shift all that code to my XML and call from there. these animations. How can I make a set of animations this way i.e Translate, fade out and another translate and fade in?



 public static void crossfade(Button buttonToFadeOut, Button buttonToFadeIn) {

buttonToFadeIn.setAlpha(0f);
buttonToFadeIn.setVisibility(View.VISIBLE);

buttonToFadeIn.animate()
.alpha(1f)
.translationY(100) //220
.setDuration(700)
.setStartDelay(60)
.setListener(null);

buttonToFadeOut.animate()
.alpha(0f)
.setDuration(700)
.translationY(-100)
.setListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
buttonToFadeOut.setVisibility(View.GONE);
}
});
}









share|improve this question

























  • Hello. Was my answer able to help you?

    – Ishaan Javali
    Nov 23 '18 at 3:10











  • @Ishaan .. Yes sorry i had to go out, replied late.

    – Muhammad Abdullah
    Nov 23 '18 at 6:49














0












0








0








I'm playing with animating my buttons, and when I click a button that button is supposed to move along the y-axis and fade out while another button will fade in on the y-axis. I've achieved this by writing the code in my java class and it works perfectly fine, but I want to shift all that code to my XML and call from there. these animations. How can I make a set of animations this way i.e Translate, fade out and another translate and fade in?



 public static void crossfade(Button buttonToFadeOut, Button buttonToFadeIn) {

buttonToFadeIn.setAlpha(0f);
buttonToFadeIn.setVisibility(View.VISIBLE);

buttonToFadeIn.animate()
.alpha(1f)
.translationY(100) //220
.setDuration(700)
.setStartDelay(60)
.setListener(null);

buttonToFadeOut.animate()
.alpha(0f)
.setDuration(700)
.translationY(-100)
.setListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
buttonToFadeOut.setVisibility(View.GONE);
}
});
}









share|improve this question
















I'm playing with animating my buttons, and when I click a button that button is supposed to move along the y-axis and fade out while another button will fade in on the y-axis. I've achieved this by writing the code in my java class and it works perfectly fine, but I want to shift all that code to my XML and call from there. these animations. How can I make a set of animations this way i.e Translate, fade out and another translate and fade in?



 public static void crossfade(Button buttonToFadeOut, Button buttonToFadeIn) {

buttonToFadeIn.setAlpha(0f);
buttonToFadeIn.setVisibility(View.VISIBLE);

buttonToFadeIn.animate()
.alpha(1f)
.translationY(100) //220
.setDuration(700)
.setStartDelay(60)
.setListener(null);

buttonToFadeOut.animate()
.alpha(0f)
.setDuration(700)
.translationY(-100)
.setListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
buttonToFadeOut.setVisibility(View.GONE);
}
});
}






android android-studio user-interface animation button






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 '18 at 5:47









Ishaan Javali

1,3402820




1,3402820










asked Nov 22 '18 at 23:49









Muhammad AbdullahMuhammad Abdullah

308




308













  • Hello. Was my answer able to help you?

    – Ishaan Javali
    Nov 23 '18 at 3:10











  • @Ishaan .. Yes sorry i had to go out, replied late.

    – Muhammad Abdullah
    Nov 23 '18 at 6:49



















  • Hello. Was my answer able to help you?

    – Ishaan Javali
    Nov 23 '18 at 3:10











  • @Ishaan .. Yes sorry i had to go out, replied late.

    – Muhammad Abdullah
    Nov 23 '18 at 6:49

















Hello. Was my answer able to help you?

– Ishaan Javali
Nov 23 '18 at 3:10





Hello. Was my answer able to help you?

– Ishaan Javali
Nov 23 '18 at 3:10













@Ishaan .. Yes sorry i had to go out, replied late.

– Muhammad Abdullah
Nov 23 '18 at 6:49





@Ishaan .. Yes sorry i had to go out, replied late.

– Muhammad Abdullah
Nov 23 '18 at 6:49












1 Answer
1






active

oldest

votes


















0














This is how you can do a fade out in XML:



<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true">
<alpha
android:duration="1700"
android:fromAlpha="1.0"
android:interpolator="@android:anim/accelerate_interpolator"
android:toAlpha="0.0" />
</set>


You have to create a new Android resource directory in res called anim and make the above file in the anim folder.



Now, to call that animation in Java you would have to do:



Animation anim = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.fadeout);


Now, to apply this animation on your button you would do:



button.startAnimation(anim);


There is a very useful video on this at the link here that explains how to make the XML files for a variety of animations. I hope that this answer was useful.






share|improve this answer
























  • Yes thats perfect. Thankyou so much

    – Muhammad Abdullah
    Nov 23 '18 at 6:49











  • But is there a way to make a set of animations on 2 buttons in one xml file . For example in your xml file you are just fading out , but i want to know if you can fade out a button and translate and then fade in another button and translate , all this code is written in a single xml file ?

    – Muhammad Abdullah
    Nov 23 '18 at 6:53











  • You will be able to gave the fading out and translate for the first button. But you will need a separate file when doing the fade in and translate of the s cond button because each animation has to be set to a certain view. By putting them all into one XML file, then the first button will do all 4 actions. So you'll have to make two separate XML files. If you look at the link I gave you, you should be able to do it. It also has a lot of other animations like translating, zooming in and others.

    – Ishaan Javali
    Nov 23 '18 at 13:37











  • Got it. Thankyou

    – Muhammad Abdullah
    Nov 23 '18 at 22:11











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53439186%2fhow-to-put-set-of-animations-on-2-buttons-through-xml%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














This is how you can do a fade out in XML:



<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true">
<alpha
android:duration="1700"
android:fromAlpha="1.0"
android:interpolator="@android:anim/accelerate_interpolator"
android:toAlpha="0.0" />
</set>


You have to create a new Android resource directory in res called anim and make the above file in the anim folder.



Now, to call that animation in Java you would have to do:



Animation anim = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.fadeout);


Now, to apply this animation on your button you would do:



button.startAnimation(anim);


There is a very useful video on this at the link here that explains how to make the XML files for a variety of animations. I hope that this answer was useful.






share|improve this answer
























  • Yes thats perfect. Thankyou so much

    – Muhammad Abdullah
    Nov 23 '18 at 6:49











  • But is there a way to make a set of animations on 2 buttons in one xml file . For example in your xml file you are just fading out , but i want to know if you can fade out a button and translate and then fade in another button and translate , all this code is written in a single xml file ?

    – Muhammad Abdullah
    Nov 23 '18 at 6:53











  • You will be able to gave the fading out and translate for the first button. But you will need a separate file when doing the fade in and translate of the s cond button because each animation has to be set to a certain view. By putting them all into one XML file, then the first button will do all 4 actions. So you'll have to make two separate XML files. If you look at the link I gave you, you should be able to do it. It also has a lot of other animations like translating, zooming in and others.

    – Ishaan Javali
    Nov 23 '18 at 13:37











  • Got it. Thankyou

    – Muhammad Abdullah
    Nov 23 '18 at 22:11
















0














This is how you can do a fade out in XML:



<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true">
<alpha
android:duration="1700"
android:fromAlpha="1.0"
android:interpolator="@android:anim/accelerate_interpolator"
android:toAlpha="0.0" />
</set>


You have to create a new Android resource directory in res called anim and make the above file in the anim folder.



Now, to call that animation in Java you would have to do:



Animation anim = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.fadeout);


Now, to apply this animation on your button you would do:



button.startAnimation(anim);


There is a very useful video on this at the link here that explains how to make the XML files for a variety of animations. I hope that this answer was useful.






share|improve this answer
























  • Yes thats perfect. Thankyou so much

    – Muhammad Abdullah
    Nov 23 '18 at 6:49











  • But is there a way to make a set of animations on 2 buttons in one xml file . For example in your xml file you are just fading out , but i want to know if you can fade out a button and translate and then fade in another button and translate , all this code is written in a single xml file ?

    – Muhammad Abdullah
    Nov 23 '18 at 6:53











  • You will be able to gave the fading out and translate for the first button. But you will need a separate file when doing the fade in and translate of the s cond button because each animation has to be set to a certain view. By putting them all into one XML file, then the first button will do all 4 actions. So you'll have to make two separate XML files. If you look at the link I gave you, you should be able to do it. It also has a lot of other animations like translating, zooming in and others.

    – Ishaan Javali
    Nov 23 '18 at 13:37











  • Got it. Thankyou

    – Muhammad Abdullah
    Nov 23 '18 at 22:11














0












0








0







This is how you can do a fade out in XML:



<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true">
<alpha
android:duration="1700"
android:fromAlpha="1.0"
android:interpolator="@android:anim/accelerate_interpolator"
android:toAlpha="0.0" />
</set>


You have to create a new Android resource directory in res called anim and make the above file in the anim folder.



Now, to call that animation in Java you would have to do:



Animation anim = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.fadeout);


Now, to apply this animation on your button you would do:



button.startAnimation(anim);


There is a very useful video on this at the link here that explains how to make the XML files for a variety of animations. I hope that this answer was useful.






share|improve this answer













This is how you can do a fade out in XML:



<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true">
<alpha
android:duration="1700"
android:fromAlpha="1.0"
android:interpolator="@android:anim/accelerate_interpolator"
android:toAlpha="0.0" />
</set>


You have to create a new Android resource directory in res called anim and make the above file in the anim folder.



Now, to call that animation in Java you would have to do:



Animation anim = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.fadeout);


Now, to apply this animation on your button you would do:



button.startAnimation(anim);


There is a very useful video on this at the link here that explains how to make the XML files for a variety of animations. I hope that this answer was useful.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 23 '18 at 0:33









Ishaan JavaliIshaan Javali

1,3402820




1,3402820













  • Yes thats perfect. Thankyou so much

    – Muhammad Abdullah
    Nov 23 '18 at 6:49











  • But is there a way to make a set of animations on 2 buttons in one xml file . For example in your xml file you are just fading out , but i want to know if you can fade out a button and translate and then fade in another button and translate , all this code is written in a single xml file ?

    – Muhammad Abdullah
    Nov 23 '18 at 6:53











  • You will be able to gave the fading out and translate for the first button. But you will need a separate file when doing the fade in and translate of the s cond button because each animation has to be set to a certain view. By putting them all into one XML file, then the first button will do all 4 actions. So you'll have to make two separate XML files. If you look at the link I gave you, you should be able to do it. It also has a lot of other animations like translating, zooming in and others.

    – Ishaan Javali
    Nov 23 '18 at 13:37











  • Got it. Thankyou

    – Muhammad Abdullah
    Nov 23 '18 at 22:11



















  • Yes thats perfect. Thankyou so much

    – Muhammad Abdullah
    Nov 23 '18 at 6:49











  • But is there a way to make a set of animations on 2 buttons in one xml file . For example in your xml file you are just fading out , but i want to know if you can fade out a button and translate and then fade in another button and translate , all this code is written in a single xml file ?

    – Muhammad Abdullah
    Nov 23 '18 at 6:53











  • You will be able to gave the fading out and translate for the first button. But you will need a separate file when doing the fade in and translate of the s cond button because each animation has to be set to a certain view. By putting them all into one XML file, then the first button will do all 4 actions. So you'll have to make two separate XML files. If you look at the link I gave you, you should be able to do it. It also has a lot of other animations like translating, zooming in and others.

    – Ishaan Javali
    Nov 23 '18 at 13:37











  • Got it. Thankyou

    – Muhammad Abdullah
    Nov 23 '18 at 22:11

















Yes thats perfect. Thankyou so much

– Muhammad Abdullah
Nov 23 '18 at 6:49





Yes thats perfect. Thankyou so much

– Muhammad Abdullah
Nov 23 '18 at 6:49













But is there a way to make a set of animations on 2 buttons in one xml file . For example in your xml file you are just fading out , but i want to know if you can fade out a button and translate and then fade in another button and translate , all this code is written in a single xml file ?

– Muhammad Abdullah
Nov 23 '18 at 6:53





But is there a way to make a set of animations on 2 buttons in one xml file . For example in your xml file you are just fading out , but i want to know if you can fade out a button and translate and then fade in another button and translate , all this code is written in a single xml file ?

– Muhammad Abdullah
Nov 23 '18 at 6:53













You will be able to gave the fading out and translate for the first button. But you will need a separate file when doing the fade in and translate of the s cond button because each animation has to be set to a certain view. By putting them all into one XML file, then the first button will do all 4 actions. So you'll have to make two separate XML files. If you look at the link I gave you, you should be able to do it. It also has a lot of other animations like translating, zooming in and others.

– Ishaan Javali
Nov 23 '18 at 13:37





You will be able to gave the fading out and translate for the first button. But you will need a separate file when doing the fade in and translate of the s cond button because each animation has to be set to a certain view. By putting them all into one XML file, then the first button will do all 4 actions. So you'll have to make two separate XML files. If you look at the link I gave you, you should be able to do it. It also has a lot of other animations like translating, zooming in and others.

– Ishaan Javali
Nov 23 '18 at 13:37













Got it. Thankyou

– Muhammad Abdullah
Nov 23 '18 at 22:11





Got it. Thankyou

– Muhammad Abdullah
Nov 23 '18 at 22:11


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53439186%2fhow-to-put-set-of-animations-on-2-buttons-through-xml%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Wiesbaden

Marschland

Dieringhausen