0

I am creating a runnable typewriter effect using a textview inside a scrollview.

this is what I did...

    final ScrollView sv = new ScrollView(this);
    sv.setLayoutParams(new LinearLayout.LayoutParams((int)display.getWidth()/2, (int)((display.getHeight()/4))*3));
    sv.setPadding(0, px2DP(10), px2DP(10), px2DP(10));

    final CharSequence text = getResources().getText(R.string.longstring);

    final TextView content = new TextView(this);
    content.setBackgroundColor(getResources().getColor(R.color.Black));
    content.setTextColor(getResources().getColor(R.color.White));
    content.setTextSize(TypedValue.COMPLEX_UNIT_SP,20);
    content.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));

    sv.addView(content);

    content.postDelayed(new Runnable(){
        @Override
        public void run() {
            content.append(text.subSequence(0, index++));
            sv.fullScroll(View.FOCUS_DOWN);     
        }

    },70);

I am only getting a black box and no text.

What am I doing wrong? Please help.

PS. I already found the solution!

daryl
  • 241
  • 1
  • 3
  • 13
  • Do you add the ScrollView to the main view? Also, what if you do a `sv.invalidate()` in the Runnable? – Jonas Schmid May 08 '12 at 18:55
  • Yeah, test to make sure everything is setup correctly. Try adding before your `content.postDelayed(...);`: `content.setText("Testing")` just to make sure the simple case displays correctly. – kabuko May 08 '12 at 18:59
  • Also, note that postDelayed runs the Runnable only once. http://developer.android.com/reference/android/view/View.html#postDelayed(java.lang.Runnable,+long) – Jonas Schmid May 08 '12 at 19:03
  • @Jonas yes the scroll view is within the onCreate of the main view. I did what you told me about invalidate(), I tried adding it before content.append -- nothing happened, before sv.fullscroll() //even tried after fullScroll -- still nothing happened. (Kabuko) I tried what you recommended. The text fully displays when it is outside the Runnable -- and will get a fullScroll after that. if the Runnable only runs once.. is there a way to iterate it? – daryl May 09 '12 at 03:47

1 Answers1

0

Solution found. I am using the handler-runnable concept wrong. Based on what Jonas pointed out, Runnable only runs once. Therefore a loop is necessary.

As suggested on this link: How to run a Runnable thread in Android?

The answer is something similar to recursion.

Anyway thanks guys! Android is so cool.

Community
  • 1
  • 1
daryl
  • 241
  • 1
  • 3
  • 13