Android Learner Android Learner - 5 months ago 11
Android Question

Animation occurs only for first time

On CLICKING on view

orhu
i am animatinng ulayout from bottom to top and on clicking on
backgroundview
i am making it invisible by animating top to bottom.But the issue is that both animation occurs only for first time i click on
orbhu
.Then no animation occurs and view just become visible and invisible without animation unless i reopen the activity.Why is this and how can i resolve this?

slide_down

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromYDelta="0%p" android:toYDelta="100%p" android:fillAfter="true"
android:interpolator="@android:anim/linear_interpolator"
android:duration="500" />
</set>


slide_up

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromYDelta="100%p" android:toYDelta="0%p"
android:fillAfter="true"
android:duration="500"/>
</set>


Code

slideDown = AnimationUtils.loadAnimation(mContext,
R.anim.slide_down);
slideUp = AnimationUtils.loadAnimation(mContext,
R.anim.slide_up);
orbhu.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ulayout.setAnimation(slideUp);
ulayout.setVisibility(View.VISIBLE);
backgroundview.setVisibility(View.VISIBLE);
}
});
backgroundview.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ulayout.setAnimation(slideDown);
backgroundview.setVisibility(View.GONE);
ulayout.setVisibility(View.GONE);
}
});

Answer

Try the below code instead

slideDown = AnimationUtils.loadAnimation(mContext,
        R.anim.slide_down);
slideUp = AnimationUtils.loadAnimation(mContext,
        R.anim.slide_up);
orbhu.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        ulayout.startAnimation(slideUp);
        ulayout.setVisibility(View.VISIBLE);
        backgroundview.setVisibility(View.VISIBLE);
    }
});
backgroundview.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        ulayout.startAnimation(slideDown);
        backgroundview.setVisibility(View.GONE);
        ulayout.setVisibility(View.GONE);
    }
});