Skip to content
Advertisement

How to display ‘thinking’ image while Java method is running on Android app

I’m coding a tic tac toe app in which the user can play against the computer. In one of the difficulties, I’ve implemented an algorithm (minimax) to find the best move, and on the first move, the function which uses this algorithm to make the move seems to take a noticeable time to run. I want to make a ‘thinking’ image pop up on the screen while the computer ‘thinks’ (executes the function). I’ve tried displaying an image before and after the function runs, but this doesn’t seem to work.

In my ‘onClick’ method which handles button pressing, this is the section of the code that is supposed to display the thinking image, make the move, and then make the thinking image invisible once the move is made:

JavaScript

where displayThink is:

JavaScript

and makeMove(difficulty) is the function that is taking the noticeable time to run.

EDIT: This is the full context of the makeMove() function which is called in the onClick function, which is the designated onClick method used to handle button presses in the activity:

JavaScript

And this is the imageView part of the xml file that deals with the image I want to display while the computer is ‘thinking’.

JavaScript

Advertisement

Answer

You could use an AsyncTask

JavaScript

EDIT:

This can be called like:

JavaScript

No arguments passed since they are optional.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement