Android Question EditText_TextChanged event raised when text is modified by code

Martin Larsen

Active Member
Licensed User
I have found that EditText_TextChanged is also called when the text is changed by code. Why is it like that? In what cases can this be useful?

The problem is that usually before showing a dialog you will fill out the fields with the existing data. Upon submission of the dialog, you will store the data again. So if you have an EditText_TextChanged sub it will be called already before showing the dialog, namely when initializing the fields in the dialog. This can lead to unexpected side effect until you somehow take precautions.

Is there a good way to handle this besides setting a flag before and after you change the text in code?
 

Martin Larsen

Active Member
Licensed User
I found a solution. In my case I was setting a dirty flag to indicate the edit text had changed. The problem was that the dirty flag was set even before showing the dialog as mentioned above. This solved it easily:

B4X:
Sub edTopic_TextChanged (Old As String, New As String)
    If pnlSettings.Visible Then edTopicDirty = True
End Sub

I still wonder why TextChanged is called when modifying the text programmatically, though?
 
Upvote 0

Martin Larsen

Active Member
Licensed User
I just checked in javascript because I was curious. Here, the onchange event is only fired by user interaction, not if you change the value in code.

It's not a big issue now that I know it. I just wonder why it is so.
 
Upvote 0

emexes

Expert
Licensed User
Consistency is usually a good thing. It is simpler to be coding up exceptions where you do want them, than patching over exceptions where you don't want them. 🍻
 
Upvote 0
Top