Search code examples
androidandroid-layouttextviewandroid-animationandroid-relativelayout

How to make an animation in relation to another element?


I am with my first application in which has two TextView (a and b). I have an animation that attempt to drop the "TextView b" at the same level of "TextView a". In my phone works perfectly, but in other cell falls in other positions. Anyone know the correct way?

enter image description here

This is the animation that I'm using:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/bounce_interpolator"
    android:fillAfter="true">

    <translate
        android:fromYDelta="0%![enter image description here][1]p"
        android:toYDelta="265"
        android:duration="1000"
        android:fillEnabled="false"
        />
</set>

Solution

  • I don't know how to do it using xml animation. But you can achieve it programmatically in the following way

    void startAnimation(){
        TextView textView1=(TextView) findViewById(R.id.textView1);
        TextView textView2=(TextView) findViewById(R.id.textView2);
    
        Animation animation=new TranslateAnimation(0, 0, 0, textView1.getBottom()-textView2.getBottom());
        animation.setDuration(1000);
        animation.setFillAfter(true);
        animation.setInterpolator(new BounceInterpolator());
        textView2.startAnimation(animation);
    }