1

I want to make a text view that scrolls down on its own slowly.

The obvious solution I can think of is using TextView or something, and make a callback that repeatedly scrolls down by a small amount and register itself in another 1/24 second, say.

Is this method highly inefficient? Is there a more battery-friendly way to achieve the same goal?

Phil
  • 5,595
  • 5
  • 35
  • 55
  • possible duplicate of [Android Animations similar to make marquee vertical](http://stackoverflow.com/questions/25731123/android-animations-similar-to-make-marquee-vertical) – samgak May 05 '15 at 02:06

1 Answers1

0

A recommended way is using the postDelay method, and stop the scrolling after the view invisible. The following code snippet is an example to implement the auto scrolling. Hope it would be helpful.

public class MainActivity extends ActionBarActivity implements Runnable {

private TextView textView;

private String[] lines = new String[] { "line 11111111111111111111111",
        "line 22222222222222222222222", "line 33333333333333333333333",
        "line 44444444444444444444444", "line 55555555555555555555555" };

private final int NUM_OF_LINES = lines.length;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    textView = (TextView) findViewById(R.id.textview);
}

/*
 * (non-Javadoc)
 * 
 * @see android.support.v4.app.FragmentActivity#onResume()
 */
@Override
protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();
    textView.postDelayed(this, 1000);
}

/*
 * (non-Javadoc)
 * 
 * @see android.support.v4.app.FragmentActivity#onPause()
 */
@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    textView.removeCallbacks(this);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

@Override
public void run() {
    // TODO Auto-generated method stub

    String[] dst = new String[NUM_OF_LINES];

    System.arraycopy(lines, 1, dst, 0, NUM_OF_LINES - 1);
    dst[NUM_OF_LINES - 1] = lines[0];

    StringBuilder builder = new StringBuilder();
    for (int i = 0; i < NUM_OF_LINES; i++) {
        builder.append(dst[i]);
        if (i != NUM_OF_LINES - 1) {
            builder.append("\n");
        }
    }

    textView.setText(builder.toString());

    lines = dst;
    textView.postDelayed(this, 1000);
}
}

And the TextView in the layout.

<TextView
    android:id="@+id/textview"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:lines="5"
    android:maxLines="5"
    android:singleLine="false"
    android:text="@string/hello_world" />
alijandro
  • 11,627
  • 2
  • 58
  • 74