Search code examples
androidandroid-layoutandroid-animationtextviewtranslate-animation

How to move a TextView to the bottom of the screen till it's gone?


I need to scroll a TextView to the bottom till it's gone by preforming an animation like translate animation in xml. I tried to create an animation file and used translate attributes in the xml, but what it will do is that it will go down by a specific distance that I set in the xml. I don't know how to let it translate to the bottom till it's gone and disappeared from the screen. One more thing is that I also want something like a listener that will perform some action when that TextView is gone and disappeared from the screen when the animation ends.

Any suggestions will be greatly appreciated. Thanks.


Solution

  • You can calculate the distance you want your view to travel, but if you don't want to be exact, you can just use the screen height:

    Display display = getWindowManager().getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    int screenHeight = size.y;
    

    And then just animate the view as needed, but add a listener to do what you want at the end:

    textView.animate().translationY(screenHeight).setListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                textView.setVisibility(View.GONE);
            }
        });
    

    You still need to make the view visibility GONE at the end in the listener, just to make sure you don't see it anymore.

    You can also try and use getBottom() for the distance.