0

I am building an Othello game( it similar to Go game) on android device by using Android Studio.

I used the minimax algorithm to build a smart bot to defeat the player. However, there are many recursive call in the minimax algorithm, so the calculation of the bot is very slow( I have tried to play and saw that it took about 30 seconds for calculation). So, I want to show a progress bar to signal the player that the bot is calculating. I have tried as follows but the progress bar did not show on my activity:

 //Each time the player goes one step, I show progress bar on activity
 // to signal to the player that the bot is calculating
 progress_bar.setVisible(View.VISIBLE);

 //Then, wait for bot's calculation. I have tried to play many time
 //and I saw that it takes about 30 seconds 
 minimax = new Minimax(chessColorMaxtrix);                           
 best_position=minimax.findBestMove();
 put(best_position); //complete bot's calculation

 //Then, I set the progress bar invisible
 progress_bar.setVisible(View.INVISIBLE);
 //And wait for the next move of the player

Further, if I do not do progress_bar.setVisible(View.INVISIBLE); then the progress_bar shows on activity normally. But it is not what I want. I want to ask, Is my progress_bar usage right or wrong? If right, why did not the progress_bar show on activity. If wrong, How can I solve may problem?

TaQuangTu
  • 2,155
  • 2
  • 16
  • 30

2 Answers2

0

Couple of things:

use Tools-> LayoutInspector to see if your progress bar is in view hierarchy. Also see z-order may be its under another view. I generally add a full screen relative layout scrim and progress bar inside it, so that taps are not allowed at time when progress bar is visible from layout. That way i am sure of z-order of my progress bar. As last item of my layout:

<RelativeLayout
    android:id="@+id/progress_bar"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:visibility="gone">
    <ProgressBar style="@style/yourStyle"/></RelativeLayout>

Toggle visibility of container

Stakshi
  • 389
  • 1
  • 15
  • Thank you! The `progress_bar` show normally if I dont set `INVISIABLE` to it. I have edited a bit in my question. – TaQuangTu Jun 06 '18 at 07:06
  • can you try showing progress bar on main thread. then do computations on other thread and when they are done spawn another thread for hiding progress bar. It is quite possible that your thread is blocked and there is no time for UI refresh – Stakshi Jun 07 '18 at 04:10
0

Try use AsyncTask for your purpose, e.g.:

YourClass {
 ...
 yourMethod () { 
   new DisplayProgressBarDuringYourOperation().execute();
 }  

  private class DisplayProgressBarDuringCalculation extends AsyncTask<Void, Void, Void> {

        /**
         * This method displays progress bar in UI thread.
         */
        @Override
        protected void onPreExecute() {
            progressBar.bringToFront();
            progressBar.setVisibility(View.VISIBLE);
        }

        /**
         * This method executes your bot calculation in background thread.
         */
        @Override
        protected Void doInBackground(Void... params) {
            //  put your bot calculation code here
        }

        /**
         * This method removes progress bar from UI thread when calculation is over.
         */
        @Override
        protected void onPostExecute(Void response) {
            progressBar.setVisibility(View.INVISIBLE);

        }
    }
}

see about AsyncTask here: https://stackoverflow.com/a/9671602/9626373

Grygorii
  • 162
  • 2
  • 6