0

I want to make a 'seeking' text which will be like: "Loading ." -> "Loading .."-> "Loading ..."

and then once I get certain data , stop this animation text and just set the text as "Found."

I tried implementing it that way :

ExecutorService threadPoolExecutor = Executors.newSingleThreadExecutor();
Future textAnimationTaskFuture;
Runnable textAnimationTask;

textAnimationTask = new Runnable() {
            @Override
            public void run() {
                int passedTime = 0;
                while(passedTime < 3000)
                {
                    if(passedTime < 1000){         
                       textView.setText("Loading.");
                    }
                    else if(passedTime >= 1000 && passedTime < 2000 )
                    {
                       textView.setText("Loading..");
                    }else if (passedTime >= 3000)
                    {
                       textView.setText("Loading...");
                    }
                    passedTime = passedTime + 1;
                    if( passedTime == 3000 )
                        passedTime = 0;
                }
            }
        };

And then I would run the process :

 textAnimationTaskFuture = threadPoolExecutor.submit(textAnimationTask);

And cancel it :

 textAnimationTaskFuture.cancel(true);
 textView.setText("Found.);

Unfortunately the loop doesn't stop on cancel(true).

BVtp
  • 2,308
  • 2
  • 29
  • 68

1 Answers1

0

I do not know exactly why it is inconsistent, but one clear problem you have is that you are trying to alter a view from a thread and not from the UI thread. In order to set the text to your view, you need to do the following:

 runOnUiThread(new Runnable() {
        @Override
        public void run() {
            textView.setText("MY_TEXT");
        }
    });

Regarding the loop not stopping - I suggest you debug it, from looking at the code I cannot detect any problem with the loop.

yakobom
  • 2,681
  • 1
  • 25
  • 33
  • tried debugging it, unfortunately cannot seem to find the problem.. Thought maybe I was doing something wrong.. :/ – BVtp Dec 29 '16 at 09:59
  • Hmmm... Take a look here, perhaps this will help you: http://stackoverflow.com/questions/13623445/future-cancel-method-is-not-working – yakobom Dec 29 '16 at 10:18