Setting the size of the canvas


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.

Here is my blocks:

u mean
when button clicked 1st - grid is incorrect
when button clicked 2nd - grid is correct

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.

send video + aia please

Link to the video:

And aia:

test.aia (5.7 KB)

1 Like

i will help soon

1 Like

Draw a Grid or Coloured Boxes on a Canvas

1 Like

really yes i for got it

but please try the link gave by @TIMAI2 it's much helpful to me

this is tough for me to identify in my search the height,width of canvas is affected each time due to drawing others will help

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 :slight_smile: 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.

Try this (without a timer): test_2.aia (5.9 KB)

1 Like

Thank you! This seems to work better. So the key here is to set both layout elements visible in designer :+1:

1 Like

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 :slight_smile:

The timer solution provided by @TIMAI2 would have also worked much faster with smaller timer interval :smiley: (Noticed that mine was the default 1s when applied that :see_no_evil:)

On two of my test devices (Galaxy Note8, Android 9 and Pixel 2XL, A11):

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.