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??