tags:

views:

213

answers:

1

I have here a button which calls a very long lasting calculation. To indicate this to the user, i want to display a loading symbol. The calculation is called in this method in a extra thread (LoadingAnimationThread);

@Override
        public void onClick(View v) {

            BookView bookView = (BookView) lexs.startBook();
            LoadingAnimationThread thread = null;
            if (type == BOOK) {
            thread = new LoadingAnimationThread(BOOK, book, bookView);
            } else if (type == CHAPTER) {
                thread = new LoadingAnimationThread(CHAPTER, chapter, bookView);
            } else if (type == PARAGRAPH) {
                thread = new LoadingAnimationThread(PARAGRAPH, paragraph, bookView);
            }

            LoadingAnimation.startAnimation();
            thread.run();
        }

The run method of the thread is the following:

@Override
        public void run() {

            if (type == BOOK) {
                ((BookView) bookView).setBookToDisplay(book);
            } else if (type == CHAPTER) {
                ((BookView) bookView).setBookToDisplay(chapter.getBook());
                ((BookView) bookView).setExpanededChapter(chapter, true);
            } else if (type == PARAGRAPH) {
                ((BookView) bookView).setBookToDisplay(paragraph.getParentChapter().getBook());
                ((BookView) bookView).setExpanededChapter(paragraph.getParentChapter(), false);
                ((BookView) bookView).setExpandedParagraph(paragraph);
            }
            LoadingAnimation.stopAnimation();
        }

This calls in the run method of the Thread are the methods which need a lot of calculation. With LoadingAnimation.startAnimation() and LoadingAnimation.stopAnimation() is start respectively stop the animation:

public static void startAnimation() {

        lexs.runOnUiThread(new Runnable() {

            @Override
            public void run() {
                loadingImage.setImageResource(R.drawable.loading_background);
                animation.start();
                Log.v("Animation", "Loading Animation started");
            }
        });

    }

    public static void stopAnimation() {

        lexs.runOnUiThread(new Runnable() {

            @Override
            public void run() {
                loadingImage.setImageResource(R.drawable.loading0);
                animation.stop();
                Log.v("Animation", "Loading Animation stopped");
            }
        });

    }

I see the Log lines in Logcat, but the animation isn't visible on the GUI. Why not??? I know the animation works properly, because on other places it works correctly. Sometimes Android doesn't update the UI immediately, that's why i used runOnUIThread(). But the doc for this method says:

If the current thread is the UI thread, then the action is executed immediately.

So i think the current thread isn't the UI thread, but how can i find the UI thread? Or how can i force the Runnable to run immediately??

A: 

You should only make a call to runOnUiThread from a background thread. I think you have the logic backwards. You should set it up so startAnimation and stopAnimation don't make any calls to runOnUiThread, just use the code as is. Then startAnimation should be called from the main UI thread and stopAnimation can be called by the background thread with runOnUiThread.

Here's some sample code:

    public void onClick(View v) {
    // this is the main thread
    // ......
    LoadingAnimation.startAnimation(); // Don't need to call this with runOnUiThread since we're on teh main thread
    thread.run();
}

@Override
public void run() {

    runOnUiThread(new Runnable() {
        // Need to put this in runOnUiThread since it's changing the UI from the background
        if (type == BOOK) {
            ((BookView) bookView).setBookToDisplay(book);
        } else if (type == CHAPTER) {
            ((BookView) bookView).setBookToDisplay(chapter.getBook());
            ((BookView) bookView).setExpanededChapter(chapter, true);
        } else if (type == PARAGRAPH) {
            ((BookView) bookView).setBookToDisplay(paragraph.getParentChapter().getBook());
            ((BookView) bookView).setExpanededChapter(paragraph.getParentChapter(), false);
            ((BookView) bookView).setExpandedParagraph(paragraph);
        }
        LoadingAnimation.stopAnimation();
    });
}

public static void startAnimation() {
    loadingImage.setImageResource(R.drawable.loading_background);
    animation.start();
    Log.v("Animation", "Loading Animation started");
}

public static void stopAnimation() {
    loadingImage.setImageResource(R.drawable.loading0);
    animation.stop();
    Log.v("Animation", "Loading Animation stopped");
}
Brandon
thanks for your hint. i implemented your approach and now the animation is displayed but then the animation frozes during the background process until stopAnimation is called
Roflcoptr
I see, I was thinking from the point of view of the ProgressBar which I guess runs in the background. It looks like you'll have to start the animation in the background where you call thread.run().
Brandon