I am creating a custom toggle switch i all most done with the design and the functionality but there is one thing blocking me, I tried to add animation on toggle to the thumb basically a button the animation towards right works perfect but the issue is with the animation towards left here is the code I am using to play both animations.
private void playToggleanimation()
{
if(toggleAnimation != null && !toggleAnimation.hasEnded())
{
toggleAnimation.setAnimationListener(null);
toggleAnimation.cancel();
}
View v = findViewById(button);
toggleAnimation = (checked) ? new TranslateAnimation(v.getLeft(), getMeasuredWidth() / 2, 0, 0) : new TranslateAnimation(v.getLeft(), 0, 0, 0);
toggleAnimation.setAnimationListener(listener);
toggleAnimation.setFillAfter(true);
toggleAnimation.setFillEnabled(true);
toggleAnimation.setDuration(250);
v.clearAnimation();
v.startAnimation(toggleAnimation);
}
Thanks for any help.
I figured it out the issue was with the
getLeft()method, instead of using this lineI used below line of code to create animation, it worked fine.
The
getLeft()method was always returning 0.