1

I have a property animation set for the alpha channels of several views which is not working as I think it should. Essentially I have 3 TextView derivatives, a TextView and two Views which I want to initially be hidden, set their data when it comes in and then animate them in. I do this with several other views in the layout and occasionally they have the same problem although it is very infrequent. Most of the time the TextView and one of the Views simple does not appear even though the data for the TextView is valid according to its Logcat output. It is always the same two views and they do rarely show up but not an any predictable manner I have been able to find. The section of the layout file and all the animation code follows. Please note that as per this question, I have set the visibility to invisible in the layout and set it to visible right before starting the animation.

EDIT

Calling `View.bringToFront() on the views which are having trouble has no visible effect. Also, not executing the animation but instead just changing the visibility of the views from invisible to visible does not show them. Removing the visibility change code and android:visibility="invisible" tag from the layout also does not make the view display.

THE LAYOUT

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:qwerjk="http://schemas.android.com/apk/res/tenkiv.app"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="8dp"
    android:paddingLeft="8dp"
    android:paddingRight="8dp" >

    <tenkiv.view.widget.MagicTextView
        android:id="@+id/hourMinuteValueTv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_toLeftOf="@+id/secondValueTv"
        android:lines="1"
        android:textSize="77sp"
        android:visibility="invisible"
        qwerjk:strokeColor="@color/light_text_outline"
        qwerjk:strokeJoinStyle="miter"
        qwerjk:strokeWidth="1.8" />

    <tenkiv.view.widget.MagicTextView
        android:id="@+id/secondValueTv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/hourMinuteValueTv"
        android:layout_toLeftOf="@+id/amPmValueTv"
        android:textSize="39sp"
        android:visibility="invisible"
        qwerjk:strokeColor="@color/light_text_outline"
        qwerjk:strokeJoinStyle="round"
        qwerjk:strokeWidth="1.3" />

    <tenkiv.view.widget.MagicTextView
        android:id="@+id/amPmValueTv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/hourMinuteValueTv"
        android:layout_alignParentRight="true"
        android:textSize="31sp"
        android:visibility="invisible"
        qwerjk:strokeColor="@color/light_text_outline"
        qwerjk:strokeJoinStyle="round"
        qwerjk:strokeWidth="1.3" />

    <tenkiv.view.widget.MagicTextView
        android:id="@+id/dateValueTv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/hourMinuteValueTv"
        android:textSize="19sp"
        android:visibility="invisible"
        qwerjk:strokeColor="@color/light_text_outline"
        qwerjk:strokeJoinStyle="round"
        qwerjk:strokeWidth="0.9" />

    <View
        android:id="@+id/lineTimeConditionSepView"
        android:layout_width="290dp"
        android:layout_height="3dp"
        android:layout_alignParentRight="true"
        android:layout_below="@id/dateValueTv"
        android:layout_marginTop="4dp"
        android:background="@color/holo_text"
        android:visibility="invisible" />

    <View
        android:id="@+id/lineTimeConditionSepViewOutline2"
        android:layout_width="290dp"
        android:layout_height="1dp"
        android:layout_alignBottom="@id/lineTimeConditionSepView"
        android:layout_alignLeft="@id/lineTimeConditionSepView"
        android:layout_marginTop="0.5dp"
        android:background="@color/location_area_dark_green"
        android:visibility="invisible" />

---THE REST OF THE LAYOUT---

THE METHODS IN MY FRAGMENT TO ANIMATE IT

public void onUpdateClock(final String time, final String seconds, final String ampm, final String date) {
    Activity activity = getActivity(); //Used to make sure we dont update the clock with the activity dead
    if (activity != null) {
        if (mHourMinuteValue == null) { //These are all used together so this should be safe
            mHourMinuteValue = (MagicTextView) activity.findViewById(R.id.hourMinuteValueTv);
            mSecondValue = (MagicTextView) activity.findViewById(R.id.secondValueTv);
            mAmPmValue = (MagicTextView) activity.findViewById(R.id.amPmValueTv);
            mDateValue = (TextView) activity.findViewById(R.id.dateValueTv);
            mTimeConditionsLine = activity.findViewById(R.id.lineTimeConditionSepView);
            mTimeConditionsOutline2 = activity.findViewById(R.id.lineTimeConditionSepViewOutline2);
            mTimeInAnim = createAnimation(CLOCK_IN);
        }
    }
    if (mHourMinuteValue == null) {
        Log.e(TAG, "Clock text views were null!");
        return;
    }
    uiHandler.post(new Runnable() {
        public void run() {
            mHourMinuteValue.setText(time);
            mSecondValue.setText(seconds);
            if (ampm != null) {
                mAmPmValue.setText(ampm);
            } else {
                mAmPmValue.setText(ClockUICallbacks.EMPTY_AMPM);
            }
            mDateValue.setText(date);
            mDateValue.bringToFront();
            if (mFadeInClock) {
                setClockVisibility(true);
                mTimeInAnim.start();
                mFadeInClock = false;
            }
        }
    });

private void setClockVisibility(boolean visible) {
        int flag;
        if (visible) {
            flag = View.VISIBLE;
        } else {
            flag = View.INVISIBLE;
        }
        mHourMinuteValue.setVisibility(flag);
        mSecondValue.setVisibility(flag);
        mAmPmValue.setVisibility(flag);
        mDateValue.setVisibility(flag);
        mTimeConditionsLine.setVisibility(flag);
        mTimeConditionsOutline2.setVisibility(flag);
    }

private AnimatorSet createAnimation(int type) {
        AnimatorSet set = new AnimatorSet();
        switch (type) {
        case CLOCK_IN:
            ValueAnimator hourMinInAnim = ObjectAnimator.ofFloat(mHourMinuteValue, "alpha", INVISIBLE, OPAQUE);
            hourMinInAnim.setDuration(FADE_IN_DURATION);
            ValueAnimator secondInAnim = ObjectAnimator.ofFloat(mSecondValue, "alpha", INVISIBLE, OPAQUE);
            secondInAnim.setDuration(FADE_IN_DURATION);
            ValueAnimator amPmInAnim = ObjectAnimator.ofFloat(mAmPmValue, "alpha", INVISIBLE, OPAQUE);
            amPmInAnim.setDuration(FADE_IN_DURATION);
            ValueAnimator dateInAnim = ObjectAnimator.ofFloat(mDateValue, "alpha", INVISIBLE, OPAQUE);
            dateInAnim.setDuration(FADE_IN_DURATION);
            ValueAnimator timeLine1InAnim = ObjectAnimator.ofFloat(mTimeConditionsLine, "alpha", INVISIBLE, OPAQUE);
            timeLine1InAnim.setDuration(FADE_IN_DURATION);
            ValueAnimator timeLine2InAnim = ObjectAnimator.ofFloat(mTimeConditionsOutline2, "alpha", INVISIBLE, OPAQUE);
            timeLine2InAnim.setDuration(FADE_IN_DURATION);
            set.playTogether(hourMinInAnim, secondInAnim, amPmInAnim, dateInAnim, timeLine1InAnim, timeLine2InAnim);
            return set;
        /*SOME OTHER ANIMATIONS*/
        }
        return null;
    }

Any other information which might be needed please let me know.

Thanks, Jared

Community
  • 1
  • 1
Jared
  • 1,449
  • 2
  • 19
  • 40

1 Answers1

0

It turns out that this was caused by a mistake in creating the layouts for two different fragments in the activity. If you look in the layout there is a MagicTextView with id dateValueTv. Well this same id existed in another layout within the activity and since both were using the activity findViewById() method to get the references sometimes they found the right view and sometimes not. I discovered this because my text view in the other fragment had the data that this problem text view should have had. All I had to do was change their id's. I am aware that I could do it based on getView().findViewById(). If this is the best practice I would love to hear comments on it but by checking if the activity is null from getActivity() and getting the views from there I have a convenient way of making sure my background thread doesn't post messages to the ui handler if the activity doesn't exist (such as during rotation).

Jared
  • 1,449
  • 2
  • 19
  • 40