I'm making a document reader app and I want a user to be able to ask the app the auto-scroll a document instead of the manual scrolling. Please could anyone give me ideas on how to make a ScrollView "Auto-Scroll" vertically? Sort of like credits at the end of a movie. I set up a TimerTask to this below but my problems are:
How do I get the increment value to feed into the TimerTask i.e the new position the scrollbar should go to each time the TimerTask is run.
Are there any better ways to achieve this auto-scroll feature?
My code is below, I achieved the auto-scroll but the scrolling wasn't even...(totally new to both Java and Android..smile)...thanks!!!
Timer scrollTimer = new Timer(); Handler handler = new Handler(); ScrollView scrollView = (ScrollView) findViewById(R.id.hse); int scrollSpeed = 5; // this is set by the user in preferences TimerTask scrollerSchedule = new TimerTask() { @Override public void run() { handler.post(new Runnable() { @Override public void run() { //Here I want to increment the scrollbar's vertical position by 10 //scrollView.smoothScrollBy(0, scrollView.getScrollY() + 10); new moveScrollView().execute(); } }); } }; scrollTimer.schedule(scrollerSchedule, 0, scrollSpeed * 100); } private class moveScrollView extends AsyncTask<Void, Void, Void> { protected void onProgressUpdate(Void... progress) { // show the user some sort of progress indicator // like how many minutes before scroll is completed } protected Void doInBackground(Void... params) { // TODO int scrollPos = (int) (scrollView.getScrollY() + 10.0); scrollView.smoothScrollTo(0, scrollPos); return null; } } Changes: Like @Kurtis suggested, I added the AsyncTask to carry out the scroll, and then used the TimerTask + Handler to do the repetition. Now when run the scroll code
int scrollPos = (int) (scrollView.getScrollY() + 10.0); scrollView.smoothScrollTo(0, scrollPos); in the AsynTask, I get an error, but when I run the code directly in my Runnable it works as expected. I'm still concerned about efficiency and would really want to use the AsynTask. Any tips on fixing my AsyncTask code?
BTW: This was my observation (for newbies like me). Previously I used
scrollView.smoothScrollBy(0, scrollView.getScrollY() + 10); and the scroll start jumping unevenly, sort of incrementing the scroll value like +5, +10 +15 +.... instead of an even repetition like +5, +5, +5.... What I found out was that if I wanted to compute the incremental value like scrollView.getScrollY() + 10, then I should have used smoothScrollTo() instead of smoothScrollBy().
scrollView.smoothScrollTo(0, scrollView.getScrollY() + 10);