3

I have a TextView in a FrameLayout as follows:

<FrameLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    ... >
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:singleLine="true"
        ... />

The text of the TextView is set in code and I have a translate animation running on the TextView that slides it left across the parent View as follows:

myTextView.setText(getVenueInformationText());
myTextView.setAnimation(AnimationUtils.loadAnimation(myContext, R.anim.slide_left_repeatedly));

I need to cater for both the cases where the text in the TextView is shorter or wider than the width of the parent View (FrameLayout in this case) . However, I am finding that the width of the animated TextView is only ever as wide as the FrameLayout (and thus my text is chopped).

Does anyone know why, when I set the text on my TextView and set the animation, the TextView appears only ever as wide as the FrameLayout (even if the text in the TextView should stretch it to be wider than the FrameLayout)?

Note 1: A marquee animation on the TextView won't do the job for me as it is possible for the text in the TextView to be smaller than the width of the parent View (in which case the marquee animation does not run... and I need the animation to run regardless of the text's width).

Note 2: Below is the xml definition of the translate animation I am using:

 <translate
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:interpolator="@android:anim/linear_interpolator"
  android:fromXDelta="100%p"
  android:toXDelta="-100%"
  android:repeatCount="infinite"
  android:duration="15000" />
Community
  • 1
  • 1
Adil Hussain
  • 30,049
  • 21
  • 112
  • 147
  • 1
    Did you try with using `LinearLayout` instead of `FrameLayout`? – Adil Soomro Jan 30 '12 at 16:47
  • Yup. But still no joy... – Adil Hussain Jan 30 '12 at 16:54
  • See if this works: http://stackoverflow.com/questions/3862409/horizontal-scrolling-text-in-android – AJcodez Jan 30 '12 at 17:17
  • 1
    That won't do it. See the note at the bottom of my question. (Appreciate the help nonetheless.) – Adil Hussain Jan 30 '12 at 17:37
  • Please, answer my question: What would you do if your text would be bigger than display's width? – teoREtik Jan 30 '12 at 17:43
  • Regardless of the width of the text (i.e. even if it's shorter or longer than the width of the FrameLayout) I want run a translate animation (which replicates a marquee animation) that slides the TextView (or the text within it if possible) left repeatedly... like a news reader. – Adil Hussain Jan 30 '12 at 18:30

2 Answers2

2

Solved it by putting my TextView in a HorizontalScrollView (instead of a FrameLayout or LinearLayout) as follows...

<HorizontalScrollView
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:paddingLeft="10dp"
  android:paddingRight="10dp"
  android:scrollbars="none" >
  <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:singleLine="true" />
</HorizontalScrollView>

... and then setting the text and animation of the TextView programmatically/dynamically as before.

Adil Hussain
  • 30,049
  • 21
  • 112
  • 147
  • I was implementing a scrollable marquee textview and found this very useful part for my final implementation. Other solutions suggested overriding the views onMeasure in combination with an AnimationListener, but using a ScrollView seems much more consistent. – ılǝ May 02 '14 at 05:56
0

So, as I understand your correctly, you want to implement something like creeping line. In this case you should dynamically download some info from a host, convert it to String and set to your TextView.

I would do this in such manner: your TextView should wrap your content by width (at least), then you use standard TranslateAnimation, beginning from the right side of parent View and ending at the (parentView.getLeft() - TextView.getWidth() - in this case your TextView would be hidden), set AnimationListener for this animation and at the end of it check if there is some new information from a source which you can set as new content of TextView and start your Animation again.

teoREtik
  • 7,886
  • 15
  • 46
  • 65
  • 1
    Thanks for the response teoREtik. See Note 2 which I've added to my question and which I believe does exactly what you've described. The width of the TextView is set to `wrap_content` and the animation is running and moving the TextView left to right repeatedly ok. The problem is that the width of the TextView never exceeds that of the FrameLayout even when the text in the TextView is wider than that of the FrameLayout... – Adil Hussain Feb 03 '12 at 10:24