0

I made a layout with some views and I need to force redraw it, because I found that invalidate isn't redrawing instantly if I use Thread.sleep().

So, I also found that i can use SurfaceHolder to achieve this, but I don't know how to use it.

If someone can explain me how to use surfaceholder with layout or just another way to force redraw a specific view from the layout.

So, I have some buttons that I change their color programmatically, but the new color is not displayed. Here is how the color is changed:

    while(array[k] < 4) {
        while(!running) {
            Thread.sleep(100);
        }
        array[k] ++;
        Position(k);
        if(Validation(k)) {
            buttons[solution[k].y][solution[k].x].setBackgroundColor(getResources().getColor(R.color.moving));
            buttons[solution[k].y][solution[k].x].forceLayout();
            if(solution[k].x == 0 || solution[k].x == width - 1 || solution[k].y == 0 || solution[k].y == height - 1) {
                DisplaySolution(k);
                Thread.sleep(300);
                ResetMatrix();
            } else {
                Bkt(k + 1);
            }
        } else {
            buttons[solution[k].y][solution[k].x].setBackgroundColor(getResources().getColor(R.color.unpressedEmpty));
        }
    }

I did console log between these and it is executing very well, except that the new color(named "moving") isn't displayed. The ResetMatrix() function resets the colors, but that shouldn't affect this. I also tried with buttons[solution[k].y][solution[k].x].invalidate();, but didn't work.

iulyus01
  • 37
  • 8
  • Show some more code in order to get a good answer. – Andrew Mortimer Nov 14 '17 at 19:11
  • Are you running you're code from the Main thread? When you update a view, it doesn't get redrawn until the main thread it free. If the main thread is sleeping, it is not free to redraw the view. Invalidate simply marks the view as needing a redraw but doesn't redraw it synchronously. You should look at using a delayed handler or something instead of Thread.sleep. – Jon Nov 14 '17 at 23:56
  • Yes. Also, I just tried the delayed handler and it's working fine. Thank you. – iulyus01 Nov 15 '17 at 15:19
  • But now I have another problem. When I was using Thread.sleep(), the code from after the thread wouldn't not execute until the sleep ends. And with delayed handler, the code from after the handler executes and after the delay have passed, it executes the delayed code... How can I make it stop for a while and the .invalidate to still work? – iulyus01 Nov 15 '17 at 19:26

0 Answers0