0

Android newb here. Please use small words :-)

I'd like to simulate typewriter output on my Android. The output being displayed is generated by a game and is somewhat freeform. The effect I want to see individual characters appear at a rate of about 6 characters a second. When a 'carriage return' is seen, I'd like to insert a delay then resume typing on the left.

What are some suggestions on views? Would the view of choice for this be a TextView? Even that seems like overkill for this read-only coarsely scrolling output.

I saw something on this thread about an AsyncTask. That looks useful. Perhaps my game will write to some manner of buffer, and a subclass of AsyncTask will pull a character out every .15 seconds or so, add it to the TextView, then invalidate() the TextView? Sound like a plan?

Resolution:

I wrapped the TextView in the ScrollView and it worked fine. The suggested 'TextView.append()' also did the job, as expected. I had trouble with the scroll.FullScroll(), however. Apparently this has to be executed using a Runnable from the scroll.post() method. I don't know why, exactly, but I'll dig into that later. There are a number of SO threads about it.

Community
  • 1
  • 1
Tony Ennis
  • 12,000
  • 7
  • 52
  • 73

1 Answers1

2

Would the view of choice for this be a TextView?

A TextView wrapped in a ScrollView seems likely:

  <ScrollView android:id="@+id/scroll"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
  >
    <TextView android:id="@+id/transcript"
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
    />
  </ScrollView>

Sound like a plan?

You do not need an AsyncTask for this. Use postDelayed() on your TextView or something to schedule a Runnable to run in 166 milliseconds. That Runnable will:

  • Append your character to your TextView
  • Scroll the ScrollView to the bottom (in case you have exceeded what fits on the screen)
  • Schedules itself via postDelayed() to run in another 166 milliseconds

The first two bullets would look like:

transcript.append(yourCharacter);
scroll.fullScroll(View.FOCUS_DOWN);

Things will not be completely even, as that 166ms is a minimum time before the Runnable runs. OTOH, slightly irregular delivery of keys would accentuate the typewriter effect.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • I see that TextView has a scrolling facility. What's the value in wrapping it in ScrollView? – Tony Ennis Apr 02 '12 at 00:02
  • @TonyEnnis: I am not sure what "scrolling facility" you are referring to. All widgets have properties and methods related to scrolling, but they do not scroll themselves -- they need to be in a scrollable container. You *might* be able to avoid the `ScrollView` with appropriate settings for the `TextView's` gravity, but then the user would not be able to scroll up to read the rest. – CommonsWare Apr 02 '12 at 11:15
  • According to the ScrollView doc at http://developer.android.com/reference/android/widget/ScrollView.html, "The TextView class also takes care of its own scrolling, so does not require a ScrollView, but using the two together is possible to achieve the effect of a text view within a larger container." I'll reread it this evening. – Tony Ennis Apr 02 '12 at 11:19
  • @TonyEnnis: They must have added that sentence within the past couple of years... :-) Feel free to give it a shot. – CommonsWare Apr 02 '12 at 11:26
  • It didn't work because to make a TextView scroll one needs to define a scrolling method: _TexView.setMovementMethod(new ScrollingMovementMethod())_ If instead one wraps it in a ScrollView then the definition isn't needed. I did it the way you suggested and it worked fine. – Tony Ennis Apr 03 '12 at 11:14
  • Inspired by the discussion over at http://stackoverflow.com/questions/6700374/android-character-by-character-display-text-animation/6700718#6700718 I created a gist of a TypewriterView that can animate typing and deleting, pause and run arbitrary Runnables. Glad to share if it can help anyone https://gist.github.com/claudijo/0cf9f43705efadfeb852 – Claudijo Nov 19 '15 at 09:46