Android Question How to Handle evetn of editbox?

mikhatri

Member
Hello,

I have some confusion regarding the EditText control in B4A for the focus_changed event.

I am looking for create a public function in my utility class and call it on a B4XPage where i needed. I want to access the EditText focus_changed event via this function to change the border color when the EditText is focused.

Below is a my suggession code for better understanding. Remember, this is not actual code but a conceptual representation of my requirement.

For example, my class name is "Utility" and my function name is "StyleEditText":

Code of function:
' Code of Utility Class

Sub Process_Globals

End Sub



' Code of StyleEditText Utility Function

Public Sub StyleEditText(parent As B4XView)

    For Each v As B4XView In parent.GetAllViewsRecursive

        If TypeOf(v) Is EditText Then

            v.AddHandler("EditText_FocusChanged", "EditText_FocusChanged_Event")

        End If

    Next

End Sub



Sub EditText_FocusChanged_Event(HasFocus As Boolean)

    Dim et As EditText = Sender

    If HasFocus Then

        Log($"${et.Text} gained focus"$)

        Dim cd As ColorDrawable

        cd.Initialize2(xui.Color_White, DipToCurrent(8), DipToCurrent(2), xui.Color_ARGB(255, 255, 0, 0))

        et.Background = cd

    Else

        Log($"${et.Text} lost focus"$)

        Dim cd As ColorDrawable

        cd.Initialize2(xui.Color_White, DipToCurrent(8), DipToCurrent(2), xui.Color_Transparent)

        et.Background = cd

    End If

End Sub
Please let me know how to implement it in my project...

Thank you.
 

teddybear

Well-Known Member
Licensed User
Hello,

I have some confusion regarding the EditText control in B4A for the focus_changed event.
For example, my class name is "Utility" and my function name is "StyleEditText":
I don't what your utility class is, if it is a code module, and then it can not handle events
 
Upvote 0

teddybear

Well-Known Member
Licensed User
The utility is a separate public class that includes many functions, which I have utilized throughout my entire project wherever needed.
I mean it is a code module or class module, how did you add the module? (Project->Add New Module...)
 
Upvote 0

teddybear

Well-Known Member
Licensed User
I got what you mean now, you don't need to add a class to do this, you only need to define the event name of all EditText views as EditText on designer.

The FocusChanged event will work for you.

B4X:
Private Sub EditText_FocusChanged (HasFocus As Boolean)
    Dim et As EditText = Sender
    Log(HasFocus)
    If HasFocus Then

        Log($"${et.Text} gained focus"$)

        Dim cd As ColorDrawable

        cd.Initialize2(xui.Color_White, DipToCurrent(8), DipToCurrent(2), xui.Color_ARGB(255, 255, 0, 0))

        et.Background = cd

    Else

        Log($"${et.Text} lost focus"$)

        Dim cd As ColorDrawable

        cd.Initialize2(xui.Color_White, DipToCurrent(8), DipToCurrent(2), xui.Color_Transparent)

        et.Background = cd

    End If

End Sub
 
Last edited:
Upvote 0

mikhatri

Member
This code works nicely for a specific EditText. I don't want to write code for each EditText in the Root, but rather for all EditText controls within a particular Root at once.
 
Upvote 0

teddybear

Well-Known Member
Licensed User
This code works nicely for a specific EditText. I don't want to write code for each EditText in the Root, but rather for all EditText controls within a particular Root at once.
This code is for all EditTexts in the Root
Project is attached.
 

Attachments

  • Project.zip
    14.5 KB · Views: 9
Upvote 0

mikhatri

Member
This code is for all EditTexts in the Root
Project is attached.
Superb, this is working very well.

However, is it possible to handle this via a Utility Class?

Because I want to code only "Utility.EditFocus(Root)" on each page in the B4XPage_Created event, and have the EditFocus function in the Utility class fetch all EditText views in Root and apply the Focus_Changed event.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…