AlphaAnimation does not work AlphaAnimation does not work android android

AlphaAnimation does not work


The problem is in android:alpha="0". This property sets transparency of a View http://developer.android.com/reference/android/view/View.html#attr_android:alpha

When alpha property is equal to 0 then animation is changing transparency from 0*0.0f=0 to 0*1.0f=0. When alpha property is set to 1 then animation is changing transparency from 1*0.0f=0 to 1*1.0f=1. That's why in first case you can't see text and in the second everything works as expected.

To make things work you have to set visibility property to invisible in layout xml. And before starting alpha animation call tv.setVisibility(View.VISIBLE);


More simple way is presented in this answer:

tv.animate().alpha(1).setDuration(1000);


actually, android have TWO alpha property for a view

    /**     * The opacity of the View. This is a value from 0 to 1, where 0 means     * completely transparent and 1 means completely opaque.     */    @ViewDebug.ExportedProperty    float mAlpha = 1f;    /**     * The opacity of the view as manipulated by the Fade transition. This is a hidden     * property only used by transitions, which is composited with the other alpha     * values to calculate the final visual alpha value.     */    float mTransitionAlpha = 1f;/** * Calculates the visual alpha of this view, which is a combination of the actual * alpha value and the transitionAlpha value (if set). */private float getFinalAlpha() {    if (mTransformationInfo != null) {        return mTransformationInfo.mAlpha * mTransformationInfo.mTransitionAlpha;    }    return 1;}

the view final alpha is the product of the TWO alpha

View#setAlpha(float) & View#animate() & android:alpha -> mAlpha

AlphaAnimation -> mTransitionAlpha