2

I want to create a TextView that scroll from right to left and then disappears from left and reappears from right again. I can use animation ?? Thanks

MimmoG
  • 631
  • 3
  • 14
  • 25

4 Answers4

6

I believe you want your text view to marquee. If so, this is how I did it:

In the XML. Set the following attributes for the TextView:

android:singleLine="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:focusable="false"
android:scrollHorizontally="true"

If your TextView is within a RelativeLayout, the width or height will have to be static (i.e. 32dp). If you need dynamic, place the TextView in another View within the RelativeLayout.

In onCreate(), you need to make the TextView selected:

myTextView.setSelected(true);

Original Answer

Community
  • 1
  • 1
BobDawg
  • 61
  • 4
1

I use this code to animate my textView in recyclerView. (this code can use any whree)

in xml

<HorizontalScrollView
                android:id="@+id/scrollViewTxtAppsEn"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:focusable="true"
                android:scrollbars="none"
                android:visibility="visible"
                >

                <TextView
                    android:id="@+id/txtAppsEn"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:paddingTop="4dp"
                    android:paddingBottom="8dp"
                    android:clickable="true"
                    android:focusable="true"
                    android:gravity="center"
                    android:maxLines="1"
                    android:scrollHorizontally="true"
                    android:text=""
                    android:textColor="@color/textColorDark"
                    android:textSize="9sp"
                    />
            </HorizontalScrollView>

In adapter

 @Override
  public void onViewAttachedToWindow(@NonNull ViewHolder holder) {
    handelAnimationTextView(holder.txtAppsEn, holder.scrollViewTxtAppsEn, true);
    super.onViewAttachedToWindow(holder);
  }

private void handelAnimationTextView(TextView textView, HorizontalScrollView scrollViewTextView, boolean isRightToLeft) {
    textView.post(new Runnable() {
      @Override
      public void run() {
        if (canScroll(scrollViewTextView)) {
          if (isRightToLeft) {
            textView.startAnimation((Animation) AnimationUtils.loadAnimation(G.currentActivity, R.anim.text_view_anim_right_to_left));
          } else {
            textView.startAnimation((Animation) AnimationUtils.loadAnimation(G.currentActivity, R.anim.text_view_anim_left_to_right));
          }
        }
      }
    });
  }

  private boolean canScroll(HorizontalScrollView horizontalScrollView) {
    View child = (View) horizontalScrollView.getChildAt(0);
    if (child != null) {
      int childWidth = (child).getWidth();
      return horizontalScrollView.getWidth() < childWidth + horizontalScrollView.getPaddingLeft() + horizontalScrollView.getPaddingRight();
    }
    return false;

  }

text_view_anim_left_to_right.xml

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
           android:duration="6000"
           android:fromXDelta="-100%"
           android:interpolator="@android:anim/linear_interpolator"
           android:repeatCount="infinite"
           android:repeatMode="restart"
           android:toXDelta="100%"/>

text_view_anim_right_to_left.xml

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
           android:duration="6000"
           android:fromXDelta="100%"
           android:interpolator="@android:anim/linear_interpolator"
           android:repeatCount="infinite"
           android:repeatMode="restart"
           android:toXDelta="-100%"/>

Hope I have helped

abolfazl bazghandi
  • 935
  • 15
  • 29
0

Does this help? Found it here

<LinearLayout android:orientation="vertical">
<HorizontalScrollView>
    <LinearLayout android:orientation="horizonal">
        <Image1 />
        <Image2 />
        <Image3 />
        <Image4 />
        <Image5 />
    </LinearLayout>
</HorizontalScrollView>
<LinearLayout android:orientation="horizonal">
    <Button1 android:layout_weight="1" />
    <Button2 android:layout_weight="1" />
    <Button3 android:layout_weight="1" />
    <Button4 android:layout_weight="1" />
</LinearLayout >

Community
  • 1
  • 1
Jon
  • 3,174
  • 11
  • 39
  • 57
0

If you want scroll text while control is focused use

<TextView ... android:ellipsize="marquee" android:singleLine="true"/>

Otherwise you should implement this yourself:

  1. Using timer (or Handler) and setting offset to TextView
  2. Stole this
  3. Or maybe try this
Community
  • 1
  • 1
Ivan
  • 1,446
  • 2
  • 11
  • 25