TextView disappears after translate Animation from one linearlayout to other linearlayout

1.2k Views Asked by At

I am trying to swap two text views (which are in different linear layouts) with each other.

My requirement is little similar to the one mentioned in the below link,

Animation: Move TextView into another container

I used ObjectAnimator of "nineoldandroids" library, but the textview is getting disappeared when it moves to other layout.

Could someone please help me out on this. Thanks in advance.

Update 1:-

Now I used PropertyValuesHolder of "nineoldandroids" library to perform translate animation. After using PropertyValuesHolder, textview are visible (i.e., not disappeared) after completing animation. But the onclick function is not working after the animation.

Also I placed toast message to check the X dimension of Textviews after animation.

Then I got the following displays:- Before Animation:- for textview at first row & first column (i.e., tv11) :- 0.0 for textview at second row & second column (i.e., tv22) :- 0.0

After Animation:- for textview at first row & first column (i.e., tv11) :- 240.0 for textview at second row & second column (i.e., tv22) :- -240.0

Is there anyway, to make these text views as a child for other layouts (to where they have been moved).

Please find the below code for the same:-

  • MainActivity.java:-

    findViewById(R.id.tv22).setOnClickListener( new View.OnClickListener() {
            public void onClick(View v) {
    
                PropertyValuesHolder pvhX = PropertyValuesHolder.ofFloat("x", +240);
                PropertyValuesHolder pvhY = PropertyValuesHolder.ofFloat("y", +380);
                ObjectAnimator.ofPropertyValuesHolder(findViewById(R.id.tv11), pvhX, pvhY).setDuration(2000).start();
    
    
                PropertyValuesHolder pvhX2 = PropertyValuesHolder.ofFloat("x", -240);
                PropertyValuesHolder pvhY2 = PropertyValuesHolder.ofFloat("y", -380);
                ObjectAnimator.ofPropertyValuesHolder(findViewById(R.id.tv22), pvhX2, pvhY2).setDuration(2000).start();
                Toast.makeText(MainActivity.this, ((TextView)findViewById(R.id.tv11)).getX()+"tv11", Toast.LENGTH_SHORT).show();
                Toast.makeText(MainActivity.this, ((TextView)findViewById(R.id.tv22)).getX()+"tv22", Toast.LENGTH_SHORT).show();
    
            }
        });
    
  • activity_main.xml:-

    <LinearLayout 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"
    android:clipChildren="false"
    android:orientation="vertical"
    tools:context=".MainActivity" >
    <View
    android:layout_width="match_parent"
    android:layout_height="1dp" />
    
    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:clipChildren="false"
    android:orientation="horizontal" >
    
    <View
        android:layout_width="1dp"
        android:layout_height="match_parent" />
    
    <TextView
        android:id="@+id/tv11"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="#4374E0" />
    
    <View
        android:layout_width="1dp"
        android:layout_height="match_parent" />
    
    <TextView
        android:id="@+id/tv13"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="#4374E0" />
    
    <View
        android:layout_width="1dp"
        android:layout_height="match_parent" />
    </LinearLayout>
    
    <View
    android:layout_width="match_parent"
    android:layout_height="1dp" />
    
    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:clipChildren="false"
    android:orientation="horizontal" >
    
    <View
        android:layout_width="1dp"
        android:layout_height="match_parent" />
    
    <TextView
        android:id="@+id/tv21"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="#4374E0" />
    
    <View
        android:layout_width="1dp"
        android:layout_height="match_parent" />
    
    <TextView
        android:id="@+id/tv22"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="#4374E0" />
    
    <View
        android:layout_width="1dp"
        android:layout_height="match_parent" />
    </LinearLayout>
    
    <View
    android:layout_width="match_parent"
    android:layout_height="1dp" />
    
    </LinearLayout>
    
  • Update 2:-

Even after performing below changes, onclick is not working fine. Could someone please help me.

        findViewById(R.id.tv22).setOnClickListener( new View.OnClickListener() {
            public void onClick(View v) {

                Toast.makeText(MainActivity.this, ((TextView)findViewById(R.id.tv11)).getX()+"tv11"+((TextView)findViewById(R.id.tv22)).getX()+"tv22", Toast.LENGTH_SHORT).show();                  

                PropertyValuesHolder pvhX = PropertyValuesHolder.ofFloat("x", +240);
                PropertyValuesHolder pvhY = PropertyValuesHolder.ofFloat("y", +380);
                final ObjectAnimator changeIn = ObjectAnimator.ofPropertyValuesHolder(findViewById(R.id.tv11), pvhX, pvhY).setDuration(2000);
                changeIn.addListener(new AnimatorListenerAdapter() {
                    public void onAnimationEnd(Animator anim) {                 
                        view.setScaleX(1f);
                        view.setScaleY(1f);*/                                                                                   
                        TextView testtv1= (TextView)findViewById(R.id.tv11);                            
                        TextView testtv2= (TextView)findViewById(R.id.tv22);
                        testtv1.clearAnimation();
                        testtv2.clearAnimation();
                        View v1= container1.getChildAt(1);
                        View v2= container2.getChildAt(3);
                        Log.i("childcount1 before",container1.getChildCount()+"");
                        container1.removeView(v1);
                        Log.i("childcount1 after",container1.getChildCount()+"");
                        Log.i("childcount2 before",container2.getChildCount()+"");
                        container2.removeView(v2);
                        Log.i("childcount2 after",container2.getChildCount()+"");
                        Log.i("check1","Removed Successfully");

                        LinearLayout.LayoutParams lp = new    LinearLayout.LayoutParams(0,0);
                        container1.addView(v2, 1,lp);                           
                        container2.addView(v1, 3,lp);

                        Toast.makeText(MainActivity.this, ((TextView)findViewById(R.id.tv11)).getX()+"tv11"+((TextView)findViewById(R.id.tv22)).getX()+"tv22", Toast.LENGTH_SHORT).show();

                    }
                });
                changeIn.start();

                PropertyValuesHolder pvhX2 = PropertyValuesHolder.ofFloat("x", -240);
                PropertyValuesHolder pvhY2 = PropertyValuesHolder.ofFloat("y", -380);
                ObjectAnimator.ofPropertyValuesHolder(findViewById(R.id.tv22), pvhX2, pvhY2).setDuration(2000).start();


            }
        });
0

There are 0 best solutions below