I have a simple app which just draws a regular 9x9 grid on a canvas. Canvas is on one layout which is made visible (and grid is drawn) with a button on another layout. When the button is first clicked, the size of the canvas is 0 x 0 according to the notification "debug print" (and grid drawn incorrectly) even though the size is set to so that it fits the screen in screen1.initiliaze. When the button is clicked for the second time the canvas size is correct and the grid is drawn correctly. Am I doing something wrong here or why this might happen? Canvas height and width are set to fill parent in designer as well as the layout element where the canvas lies.
Yes. After the 1st click, I return to the first layout with phone's back button and click the button again and everything works. Grid being incorrect probably relates to the size of the canvas which seems to be 0 x 0 at 1st click but 390 x 390 at 2nd click according to the notification at the beginning of the drawGrid procedure.
Timer seems to be the solution. That just makes quite big delay in the drawing (check the video below) but maybe that is a feature not a bug Thank you!
Drawing a grid in my final app:
I also had to add timer to make the grid visible because otherwise user could paint those colored boxes before the grid was actually drawn correctly.
One more comment to this. I applied this method to my final app and it works, but the top layout (the one with the button in the test_2 example) does not cover the whole screen even though the height is set to 100%. Some pixels of the grid layout element is visible at the top of the screen and user can actually touch the grid.
Solved this with 100ms timer so that the canvas layout is made invisible at app start before user can spot it
The timer solution provided by @TIMAI2 would have also worked much faster with smaller timer interval (Noticed that mine was the default 1s when applied that )