shafeeq
shafeeq

Reputation: 1599

Android - Animation from invisible to visible

I was trying to animate a text view with the following code

layout/fragment_testing.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.onthego.OnTheGoFragment">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:text="Heading test"
        android:gravity="center"
        android:layout_marginTop="150dp"
        android:textAlignment="center"
        android:textColor="@android:color/black"
        android:textSize="@dimen/heading_level_2"
        android:id="@+id/otg_main_title_tv"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="@dimen/otg_main_menu_item_height"
        android:layout_marginTop="10dp"
        android:layout_marginStart="@dimen/otg_main_menu_item_magin_side"
        android:layout_marginEnd="@dimen/otg_main_menu_item_magin_side"
        android:id="@+id/otg_main_rapid_test_tv"
        android:text="Rapid Test"
        android:textSize="@dimen/text_size_1"
        android:textColor="@android:color/black"
        android:textAlignment="center"
        android:gravity="center"
        android:background="@drawable/otg_menu_item_bg"
        android:layout_below="@id/otg_main_title_tv"/>

    <ImageView
        android:layout_width="@dimen/otg_main_menu_item_height"
        android:layout_height="@dimen/otg_main_menu_item_height"
        android:layout_alignTop="@id/otg_main_rapid_test_tv"
        android:layout_alignStart="@id/otg_main_rapid_test_tv"
        android:background="@drawable/otg_menu_item_bg"
        android:padding="@dimen/otg_main_menu_item_icon_padding"
        android:src="@drawable/ic_chevron_right_black_48dp"/>

</RelativeLayout>

Testfragment.java

public class Testfragment extends Fragment {

    private TextView mTvOtgRapidTest;
    private Activity    mActivity;
    private Context     mContext;

    public Testfragment () {
        // Required empty public constructor
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if(mContext == null) {
            mContext = context;
            mActivity = (MainActivity) getActivity();
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_testing, container, false);
        mTvOtgRapidTest = (TextView) root.findViewById(R.id.otg_main_rapid_test_tv);

        new Handler().postDelayed(new Runnable() {  //To ensure if the view is visible on fragment attach
            @Override
            public void run() {
                setMenuAnimation();
            }
        }, 2000);

        return root;
    }


    private void setMenuAnimation() {
        Animation menuItemAnimation = AnimationUtils.loadAnimation(mActivity, R.anim.menu_item_scale_x_up);
        mTvOtgRapidTest.startAnimation(menuItemAnimation);

        mTVOtgStamina.startAnimation(menuItemAnimation);
    }

}

Here what I am trying to do is, the view should be invisible on attaching the fragment and after the animation, It should be visible.

But what happening here is the view will be visible on attaching the fragment, and start animation.

How I make the text view from invisible state to visible by animation?

Upvotes: 0

Views: 1104

Answers (2)

Praveen Thirumurugan
Praveen Thirumurugan

Reputation: 1077

In the parent activity of the element you need to animate add the following attribute and the animation will be done automatically.

android:animatelayoutchanges="true"

Upvotes: 1

Elias Fazel
Elias Fazel

Reputation: 2123

in xml make it invisible

android:visibility="invisible"

whenever you want start animation call it and add listener to animation and at end of animation set it make it visible

textView.startAnimation(anim);
anim..setAnimationListener(new Animation.AnimationListener() {
    @Override
    public void onAnimationStart(Animation animation) {

    }

    @Override
    public void onAnimationEnd(Animation animation) {
           **textView.setVisibility(View.VISIBLE);**
    }

    @Override
    public void onAnimationRepeat(Animation animation) {

    }
});

Upvotes: 1

Related Questions