Android Question Listener to Properties

Guenter Becker

Active Member
Licensed User
Hello, hope you are fine.

Sometimes I have to change a custom property value of a custom control by code from the controls parent. This is quite easy. But if I do so only the value of the property is changed.
I like to have a property listener that triggers a sub if the custom property value changes like an event does.

Is that possible in B4A and if yes hoiw. Thank you.
 

klaus

Expert
Licensed User
Longtime User
Are you speaking of customviews you have developed or in general.
If they are yours you can define property routines with getProperty and setProperty and put any code in theses routines.
If they are not yours, the only chance is that the developer of these customviews includes the property routines.
 
Upvote 0

Computersmith64

Well-Known Member
Licensed User
Longtime User
There is a way to implement a callback on properties in Kotlin using Delegates.observable - eg:
B4X:
var validScore by Delegates.observable(false) { _, _, newValue ->
    if (newValue) {
        scoreView.setBackgroundResource(R.drawable.score_bg_orange)
    } else {
        scoreView.setBackgroundResource(R.drawable.score_bg_white)
    }
}
I guess you could write the same thing in inline Java in B4A - but I'm not 100% sure...

https://kotlinlang.org/docs/reference/delegated-properties.html

- Colin.
 
Upvote 0

Guenter Becker

Active Member
Licensed User
Hello,
thank you for the response. 1st I'm talking about my own controls. Maybe that my question was not well. Lets ask me in general. I need a way to infor my code if the value of a variable/global has changed. For Example: in the control I have the global or property 'color'. If I change the property by setting it's value form the parent code I like to react on this change inside the controls code. Thats why I looking for a listener that goes activ and calls a sub like an event does if triggert.
 
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
Change from
B4X:
Public Value As Int
to
B4X:
Private MyValue As Int

Public Sub getValue As Int
    ' my code
    Return MyValue
End Sub

Public Sub setValue(V As Int)
    MyValue=v
    ' my code
End Sub

It will appear like this, as if it were a variable, but in reality it is a method / property
B4X:
MyClass.Value=!0
log(MyClass.Value)
Set and get must be lowercase
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
You can add properties to you cutomviews, like below.
B4X:
Public Sub setGraphColor(GraphColor As Int)
    Graph.GraphColor = GraphColor
    DrawGraph
End Sub

'Gets or sets the graph grid color
Public Sub getGridColor As Int
    Return Graph.GraphColor
End Sub

And use them outsides:
B4X:
xGraph1.GridColor = xui.Color_Gray

The two prefixes must be lower case !
'set' means writable and 'get' means readable.

You might have a look at chapter 4.4 Add properties in the B4X Customviews booklet.
 
Upvote 0

Guenter Becker

Active Member
Licensed User
Oh yes! this is what is also in my mind. But in the past I worked with .Net and I was able to create a listener for this. Seems to me that there is non in B4A. Thank you for your answers.
 
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
Oh yes! this is what is also in my mind. But in the past I worked with .Net and I was able to create a listener for this. Seems to me that there is non in B4A. Thank you for your answers.
see this.
 
Upvote 0

Guenter Becker

Active Member
Licensed User
Hi stardust that looks good never heard about that. Will go to try the file in the Observable Article. Thank you.
 
Upvote 0

Guenter Becker

Active Member
Licensed User
Hi, now I examined the Demo Project of Observarables. Wow, that is exactly I am looking for. Makes live of developing database driven apps much easier. Helps me to develop my new data bound custom controls. Thank you very much. ???
 
Upvote 0
Top