Changing values of labels

James Bobby

New Member
Licensed User
Longtime User
Hi,
I have a "static" label, created in the Designer and declared in Globals - so it's not created at rumtime in a sub.

I have a TabHost, one tab has the layout created in the Designer and is loaded when an item is clicked, after it is loaded the label gets assigned its text.

When I then go back and click another item, the label gets assigned the new value but the old value is never redrawn/refreshed/changes making the label ending up with "double" text - see attached screenshot.

I've tried to invalidate both the label, the panel but just can't seem to get the label to refresh properly. There's also an ImageView with the very same design as the label and the image changes properly.

Any ideas, or am I forced to create the labels at runtime?

/jb
 

Attachments

  • label.png
    label.png
    2.4 KB · Views: 182

Erel

B4X founder
Staff member
Licensed User
Longtime User
or am I forced to create the labels at runtime?
No. There is no problem with updating any label.

It is difficult to say what causes this issue without seeing your code. My guess is that you are creating multiple labels instead of updating the correct label. Note that you should not initialize labels added with the designer.
 
Upvote 0

James Bobby

New Member
Licensed User
Longtime User
Would a LoadLayout duplicate labels? As I think my code might call LoadLayout for every time it tried to load the new panel.
 
Upvote 0

MarkBasik4Android

Member
Licensed User
Longtime User
..the label gets assigned the new value but the old value is never redrawn/refreshed/changes making the label ending up with "double" text - ....
/jb

I Have the same problem!

I Have:
B4X:
...
Dim loading As Label
....
loading.Text = "Loading..."
....
loading.Text = "...."
...
loading.Text = "done"
....

After the first loading.Text = "Loading..." the text become "Loading..."
after... it does not change or overlap with new texts I add

:sign0085:
 
Upvote 0

MarkBasik4Android

Member
Licensed User
Longtime User
Can you post the complete sub code?

Here it is....:D

B4X:
'Activity module
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.

End Sub

Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.

   Dim ImageView1 As ImageView   
   Dim Animation1() As String
      Animation1 = Array As String ("00","01", "02", "03", "04", "05", "06", "07", "08", "09", "10","11", "12", "13", "14", "15", "16", "17", "18", "19", "20","21", "22", "23", "24", "25", "26", "27", "28", "29", "30","31", "32", "33", "34", "35", "36", "37", "38", "39", "40","41", "42", "43", "44", "45", "46", "47", "48", "49", "50","51", "52", "53", "54", "55", "56", "57", "58", "59", "60")      
   Dim Animation1MAX As String
      Animation1MAX = 40
   Dim Animation1Velocity As String   
      Animation1Velocity  = 40 ' milli seconds
   Dim Animation1Start As String
   Animation1Start = 0
   Dim TimerMario1 As Timer
   
   
   Dim ImageView2 As ImageView   
   Dim Animation2() As String
      Animation2 = Array As String ("00","01", "02", "03", "04", "05", "06", "07", "08", "09", "10","11", "12", "13", "14", "15", "16", "17", "18", "19", "20","21", "22", "23", "24", "25", "26", "27", "28", "29", "30","31", "32", "33", "34", "35", "36", "37", "38", "39", "40","41", "42", "43", "44", "45", "46", "47", "48", "49", "50","51", "52", "53", "54", "55", "56", "57", "58", "59", "60")      
   Dim Animation2MAX As String
      Animation2MAX = 19
   Dim Animation2Velocity As String   
      Animation2Velocity  = 40 ' milli seconds
   Dim Animation2Start As String
   Animation2Start = 0
   Dim TimerMario2 As Timer


   Dim www As Label
   Dim loading As Label
   
End Sub

Sub Activity_Create(FirstTime As Boolean)
   If FirstTime Then
      Activity.LoadLayout("introneoart2")
      loading.Text = "Loading..."
         TimerMario1.Initialize("TimerMario1", Animation1Velocity) 'show it in 1 sec
         TimerMario1.Enabled = True
   End If
   
         TimerMario2.Initialize("TimerMario2", Animation2Velocity) 'show it in 1 sec
         TimerMario2.Enabled = True
   
End Sub


Sub TimerMario1_Tick
   If Animation1Start <= Animation1MAX Then
      ImageView1.Bitmap  = LoadBitmap(File.DirAssets, "intro-"&Animation1(Animation1Start)&".png")
      Animation1Start = Animation1Start + 1
      TimerMario1.Initialize("TimerMario2", Animation2Velocity) 'show it in 1 sec
      TimerMario1.Enabled = True ' Again?
   Else If Animation1Start > Animation1MAX Then
      loading.Text = "Ready - Tap to start"
      'Animation1Start = 1 ' This is for LOOP
      TimerMario1.Initialize("TimerMario2", Animation2Velocity) 'show it in 1 sec
      TimerMario1.Enabled = False ' STOP
      
   End If
End Sub


Sub TimerMario2_Tick
   If Animation2Start <= Animation2MAX Then
      ImageView2.Bitmap  = LoadBitmap(File.DirAssets, "anim-"&Animation2(Animation2Start)&".png")
      Animation2Start = Animation2Start + 1
      TimerMario2.Initialize("TimerMario2", Animation2Velocity) 'show it in 1 sec
      TimerMario2.Enabled = True ' Again?
   Else If Animation2Start > Animation2MAX Then
      loading.Text = "Ready - Tap to start"
      TimerMario2.Initialize("TimerMario2", Animation2Velocity) 'show it in 1 sec
      Animation2Start = 0 ' This is for LOOP
      'TimerMario1.Enabled = False ' STOP
      
   End If
End Sub


Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)
loading.Text = "Start!"
End Sub

and if you can help I have also problem with the
TimerMario2.Initialize ... I have to put it every time I use:
TimerMario2.Enabled = True

Thanks
 
Upvote 0

MarkBasik4Android

Member
Licensed User
Longtime User
Activity.LoadLayout should be called each time the activity is created. Not only when FirstTime is true.


so I have to write

Activity.LoadLayout("xxxxxxxxxxxxxxx")
loading.Text = "Loading..."
each time I change:
loading.Text = "something."

or



loading.Text = "Loading..."
Activity.LoadLayout("xxxxxxxxxxxxxxx")

?​
 
Upvote 0

MarkBasik4Android

Member
Licensed User
Longtime User
No, you should only call it once in Activity Create without the First Time flag. Afterwards, you can change the text as many times as you need. However, your labels should be also defined in Sub Globals.



Sent from my HTC Desire using Tapatalk

So you mean that
AFTER:
B4X:
Sub Activity_Create(FirstTime As Boolean)
    If FirstTime Then
        Activity.LoadLayout("xxxxxxxxxxxxx")
        loading.Text = "Loading..."
            TimerMario1.Initialize("TimerMario1", Animation1Velocity) 'show it in 1 sec
            TimerMario1.Enabled = True
    End If

I have to write again:

B4X:
 Activity.LoadLayout("xxxxxxxxxxxxx")
....

End Sub

I will try......
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
No,like this:
B4X:
Sub Activity_Create(FirstTime As Boolean)
  If FirstTime Then
    TimerMario1.Initialize("TimerMario1", Animation1Velocity) 'show it in 1 sec
    TimerMario1.Enabled = True
  End If
  Activity.LoadLayout("xxxxxxxxxxxxx")
  loading.Text = "Loading..."
Best regards.
 
Upvote 0

Penko

Active Member
Licensed User
Longtime User
As Klaus has said, you have to put Activity.LoadLayout outside the FirstTime if statement.

FirstTime - tells you whether it is the firsttime your activity is started. This happens when the application has previously been killed by OS or it is really the very first time it is started(fresh install).

Loading layouts is compulsory every time Activity_Create is called. Otherwise, you will get a blank black screen.

It is exactly Klaus who has written the two PDF files, check the Activity tutorials. He has explained it very good.
 
Upvote 0
Top