Update a label inside an event

Skip to first unread message
Assigned to halatmi...@gmail.com by me


Feb 8, 2014, 7:26:11 PM2/8/14
to mitappinv...@googlegroups.com
Hello everyone, this is Saverio.

I'm using:
Windows7 sp1
ai2 app inventor
ai2 companion
wifi connection
samsung galaxy note 2
android 4.1.2
firefox 23.0.1

I have a label with Height and Width properties both set in automatic.
Inside an event, for example a button click, I need to to change the
text and the font size of the label and then get the width of it.
Now what happen is that I have to push 2 time the button until will get
the correct value of the label width.

I can suppose this happen because the system can't update the label while inside an event. After several tries, I convinced myself that there is
need of something like DoEvents in Visual Basic.
The file attached "B1.png" show what is not working.
Instead, the code in "B3.png" is working but, for me, useless because
I need to run the code inside an event.
A workaround could be to use a timer and call the function asynchronously.
But I would use this solution only if there is nothing else to do.

Please, provide some suggestion...




Feb 9, 2014, 3:13:22 AM2/9/14
I'd create a proc to do all of that for you in p1.png for example, and then check the width when it comes back to the button click event.

Things in AI don't exactly follow the order they do as you lay out the blocks, because many of these things are calls to system functions which act on their own after getting the commands, and act at the same time as other stuff is processing.

Try taking everything, including the local variable declaration block, except for the last two blocks, and move it all into a proc.  Call the procedure from the button click, then do the two last blocks... everything else in the block.  The movement from the proc back to the click may work as a DoEvents or Refresh in VB...

Try that first and if it doesn't help, we'll try to find another way to do it. But PLEASE let us know one way of the other...

Also, update your Firefox... they're on 26 now...
Message has been deleted


Feb 9, 2014, 7:22:00 PM2/9/14
to mitappinv...@googlegroups.com, sav...@gmail.com
Thank you for reply.
hmmm, it seems a good challenge, I like it

So, basically, what I'm trying to do is to put three words,
one at beginning of the x axis less some space, one in the middle
and another one at the end of the x axis less some space.

The first and the last are easy to get but to put the one in the middle
at the exact centre there is need to know how large the text is.

I'm using a phantom label with automatic width(-2) property to get the
text width ((screen.width - text.width) / 2 ).

I can know in advantage the text width of my phone, but, I can't with
the other phones. May change the screen resolution.
I'm trying also to adjust the font size in relation with screen size.
So, I have to put all the code inside a screen.initialize event.

In the next row, with another canvas, I need to show three counters with different font size. I think is too difficulty to get that with screen arrangment and labels. I think the canvas is the best way.

Btw, I've already tried to insert the whole code in a procedure, as you
suggest in the reply, but got the same result of screen.initialize or
one button's solution.

Hope I'm clear
Now my FireFox version is 27.0

Sorry if I'm long

Thank you


Feb 9, 2014, 7:54:00 PM2/9/14
to mitappinv...@googlegroups.com, sav...@gmail.com
It would be easier to understand if you can export your project to an AIA file and post it here for us to see.  It's too hard to understand the explanation without seeing something for it to go with on the screen.
  1. Click on the My Projects link at the top of the UI.
  2. When the list of projects shows up, put a check-mark by the project you want to export.
  3. Then click on the Project link (notice that they're similarly named but not the same)
  4. Choose Export selected project (.aia) to my computer...
That's it.  The file will be saved where all of your other downloads are saved, and it will have the format YourProjectName.aia, so you can just upload that here and we'll take a look.
Message has been deleted
Message has been deleted
Message has been deleted
Message has been deleted
Message has been deleted


Feb 10, 2014, 9:51:13 AM2/10/14
to mitappinv...@googlegroups.com, sav...@gmail.com
Here is a test AIA from Saverio.  He's trying to get the width of a "phantom" label on a screen so he can use that setting to display text on a canvas.

He takes the first measurement in Screen1.Initialize, but it doesn't come back with the same numbers as if he does it in a button.

Any suggestions?



Feb 16, 2014, 8:00:27 PM2/16/14
to mitappinv...@googlegroups.com, sav...@gmail.com
Is there anyone here that can take a look at this???   I don't see why it's doing what it's doing...

Hal Abelson

Feb 16, 2014, 10:47:17 PM2/16/14
to mitappinv...@googlegroups.com, sav...@gmail.com
Issue 713

Severio is correct.

If you change the text of a label inside an event handler, the change in width of the label does not register until after the event handler has run.
I'll file this as a bug, and we'll see if we can fix it.  I don't know if we need a full VB-style do-events -- there many be another approach --but we'll think about it.

Severio, thanks for reporting this.

Hal Abelson

Feb 18, 2014, 12:35:32 PM2/18/14
to mitappinv...@googlegroups.com, sav...@gmail.com

There's an updating issue that the dev team needs to look at.   But if what you basicallty want is to have a centered label (not on a canvvas, though) you can crate a horizontal arrangement whose width is fill_parent and whose horizontal layout is "cnetered" and put the label in that.

Scott Ferguson

Feb 18, 2014, 4:55:38 PM2/18/14
to mitappinv...@googlegroups.com
I found the problem.
PhantomLabel's TextColor was set to None.
Setting it to Black now displays RIGHT on button click!
So no need to put the blocks inside a Timer block as far as I can tell.

Saverio Scaccianoce

Feb 18, 2014, 5:06:16 PM2/18/14
to mitappinv...@googlegroups.com, sav...@gmail.com
Yes, I know we can do a lot of workarounds.

In short I will explain what I'm trying to do and the difficulties
I met:

I'm trying to display some counter on a the screen just like a
LED display. The numbers are by group with their own size
(font size).

Consider this:
If you have three counters with different font size, each in their own labels and both of the label are inside an horizontal arrangment,
you will loose a lot of time, trying to get the counters at the same position like when they start. AutoSize, Fill Parent, everything is
moving following the counter progress. "10", "100", "1000" have different size.
At least there is need the possibility to set the "Top" and the "Left" properties of a control to get some result,
Even, I have tried to use a label for each single cypher, but fail.

Consider also that everything should work on every phone, since Android 2.2.
For this reason I used a Canvas to write some text, but it has a very bad font quality.

Sorry if I seem not so politically correct but this is not my mother tongue


Saverio Scaccianoce

Feb 18, 2014, 6:05:59 PM2/18/14
to mitappinv...@googlegroups.com
 No. Is not true. It doen't work.
 I tried right now, just for curiosity.

 Look at this:
    _______________ ____
     initialize global w to  200
    ---------------------------- ------

   ## when  Button1.Click
   ##        _________________ ___________
   ##  do    set Label1.Width to |>| get global w  
   ##        ------------------------------- -------------------
   ##        __^______________ ____________
   ##           set Label2.Width to |>| Label1.Width 
   ##        ------------------------------ ---------------------

 Even you change the labe background color or even the text color or what ever you want
 Still not working. On the first hit you will get the wrong result
 You have to push the button twice.

 That is true even use screen.initialize event instead a button.
 But you cannot throw screen.initialize two times as you can do with a button.


Scott Ferguson

Feb 21, 2014, 1:04:15 PM2/21/14
to mitappinv...@googlegroups.com
You said: 'moving following the counter progress. "10", "100", "1000" have different size.'
Here is the simplest solution:
Go back to the Horizontal arrangement and set your label inside to the maximum size needed ie. 75.
Set the label alignment to whatever you want.
Every number will then be aligned the same.
You can use empty labels set to fill parent to space out other labels that contain data.

Saverio Scaccianoce

Feb 21, 2014, 1:23:09 PM2/21/14
to mitappinv...@googlegroups.com
 But, I said also

" If you have three counters with different font size, each in their own labels and both of the label are inside an horizontal arrangment"
 Once I will have time I will post an example picture. So you can decide, by youself, if it's easy or not.

 By the way, that is not the main question, that should be "Update a label inside an event".
Reply all
Reply to author
0 new messages