Refreshing with invalidate

MDEnt

Member
Licensed User
Longtime User
I am trying to refresh a label when a value is updated. I haven't yet used invalidate so I made the quick sample below to play around with and learn with - with no luck. Am I not understanding or using invalidate properly?


B4X:
Sub Activity_Create(FirstTime As Boolean)
   label1.Initialize("panel1")
   button1.Initialize("button1")
   value1 = 25
   label1.Gravity = Gravity.CENTER
   label1.Text = "Value is: " & value1
   
   Activity.AddView(label1,0,0,100%x,50%y)
   Activity.AddView(button1,0,50%y,100%x,50%y)
End Sub

Sub button1_Click
   value1 = value1 - 1
   ToastMessageShow(value1,True)
   label1.Invalidate
End Sub
 

NJDude

Expert
Licensed User
Longtime User
Your code is wrong, the code below should work.
B4X:
Sub Globals
  
    Dim value1 As Int

End Sub

Sub Activity_Create(FirstTime As Boolean)
    label1.Initialize("label1")
    button1.Initialize("button1")
    value1 = 25
    label1.Gravity = Gravity.CENTER
    label1.Text = "Value is: " & value1

    Activity.AddView(label1,0,0,100%x,50%y)
    Activity.AddView(button1,0,50%y,100%x,50%y)
End Sub

Sub button1_Click
    value1 = value1 - 1

    label1.Text = value1
    ToastMessageShow(value1,True)

End Sub

INVALIDATE is used to refresh or redraw the Activity, not to assign values.
 
Upvote 0

MDEnt

Member
Licensed User
Longtime User
My understanding from the core views help is that invalidate can redraw a view as well as an activity.

I am not trying to assign a value with invalidate. That happens on:

value1 = value1 -1

If value1 has changed from the above math, I confirm with a toast message.

What I am then ultimately then trying to do is update/refresh the label with the new updated value.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…