Search code examples
javaandroidxmltextviewdrawable

Rotate TextView DrawableRight OnClick


I have a TextView with an arrow set on the right of it. I want the arrow to rotate when it is clicked and return it back to the previous position when it is clicked again to show and hide text. I have been able to show and hide the information I just can't get the rotation animation to work.

XML

<TextView
   android:id="@+id/expandable_first_next_last_air_date"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:layout_gravity="center"
   android:layout_marginBottom="8dp"                    
   android:drawableEnd="@drawable/ic_keyboard_arrow_down_white_24dp"
   android:drawableTint="?attr/textColor"
   android:text="First, Next &amp; Last Air Date"
   android:textColor="?attr/textColor"
   android:textSize="26sp" />

TextView OnClick Listener

        tvExpandableFirstNextLastAirDate = findViewById(R.id.expandable_first_next_last_air_date);

        tvExpandableFirstNextLastAirDate.setOnClickListener(v -> {
            if (isTextViewClicked){
                tvFirstNextLastAirDate.setMaxLines(0);
                isTextViewClicked = false;
            }
            else{
                tvFirstNextLastAirDate.setMaxLines(Integer.MAX_VALUE);
                isTextViewClicked = true;
            }
        });

Solution

  • java:

     final TextView textView = findViewById(R.id.text_view);
            final ImageView imageView = findViewById(R.id.image_view);
        
        final TextView textView2 = findViewById(R.id.text_view2);
        final ImageView imageView2 = findViewById(R.id.image_view2);
        final AnimationSet animSetUp = new AnimationSet(true);
        animSetUp.setInterpolator(new DecelerateInterpolator());
        animSetUp.setFillAfter(true);
        animSetUp.setFillEnabled(true);
    
        final AnimationSet animSetDown = new AnimationSet(true);
        animSetDown.setInterpolator(new DecelerateInterpolator());
        animSetDown.setFillAfter(true);
        animSetDown.setFillEnabled(true);
    
        final RotateAnimation animRotateUp = new RotateAnimation(0.0f, 180.0f,
                RotateAnimation.RELATIVE_TO_SELF, 0.5f,
                RotateAnimation.RELATIVE_TO_SELF, 0.5f);
    
        animRotateUp.setDuration(1500);
        animRotateUp.setFillAfter(true);
        animSetUp.addAnimation(animRotateUp);
    
        final RotateAnimation animRotateDown = new RotateAnimation(180.0f, 0.0f,
                RotateAnimation.RELATIVE_TO_SELF, 0.5f,
                RotateAnimation.RELATIVE_TO_SELF, 0.5f);
    
        animRotateDown.setDuration(1500);
        animRotateDown.setFillAfter(true);
        animSetDown.addAnimation(animRotateDown);
    
        final boolean[] isTextViewClickedForTextView1 = {true};
        final boolean[] isTextViewClickedForTextView2 = {true};
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (isTextViewClickedForTextView1[0]) {
                    textView.setMaxLines(0);
                    isTextViewClickedForTextView1[0] = false;
                    imageView.startAnimation(animSetUp);
                } else {
                    imageView.startAnimation(animSetDown);
                    textView.setMaxLines(Integer.MAX_VALUE);
                    isTextViewClickedForTextView1[0] = true;
    
                }
            }
        });    
        
        textView2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (isTextViewClickedForTextView2[0]) {
                    textView2.setMaxLines(0);
                    isTextViewClickedForTextView2[0] = false;
                    imageView2.startAnimation(animSetUp);
                } else {
                    imageView2.startAnimation(animSetDown);
                    textView2.setMaxLines(Integer.MAX_VALUE);
                    isTextViewClickedForTextView2[0] = true;
                }
            }
        });
    

    XML :

        <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">
    
        <TextView
            android:id="@+id/text_view"
            android:layout_width="match_parent"
            android:layout_height="45dp"
            android:layout_margin="10dp"
            android:padding="10dp"
            android:text="text view 1"
            android:textAlignment="viewStart"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    
        <ImageView
            android:id="@+id/image_view"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:padding="5dp"
            android:src="@drawable/ic_baseline_arrow_drop_down_24"
            app:layout_constraintBottom_toBottomOf="@+id/text_view"
            app:layout_constraintEnd_toEndOf="@+id/text_view"
            app:layout_constraintTop_toTopOf="@+id/text_view" />
    
        <TextView
            android:id="@+id/text_view2"
            android:layout_width="match_parent"
            android:layout_height="45dp"
            android:layout_margin="10dp"
            android:padding="10dp"
            android:text="text view 2"
            android:textAlignment="viewStart"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/text_view"/>
    
        <ImageView
            android:id="@+id/image_view2"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:padding="5dp"
            android:src="@drawable/ic_baseline_arrow_drop_down_24"
            app:layout_constraintBottom_toBottomOf="@+id/text_view2"
            app:layout_constraintEnd_toEndOf="@+id/text_view2"
            app:layout_constraintTop_toTopOf="@+id/text_view2" />
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    
     
    

    res/anim/ic_baseline_arrow_drop_down_24.xml :

        <?xml version="1.0" encoding="utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/cycle_interpolator">
        <rotate android:fromDegrees="0"
            android:toDegrees="180"
            android:pivotX="50%"
            android:pivotY="50%"
            android:duration="5000" />
    </set>