B4J Question [Solved] Custom View focus changed

Fernando Solá

Member
Licensed User
Longtime User
Hello!

I'm creating a custom view with a couple of textfields and a couple of imageviews in it. I need to make it as a custom view since there will be several of these views on each of the different forms on this project. There will only be one textfield visible at any time and it may switch from one to the other at any moment when one of the imageviews gets clicked. Both textfields are editable and their content is somewhat linked between them. In any case, I need to know when the focus is on this custom view as well as when the custom view has lost the focus to be able to trigger a "focus event". Is there any way to get the focus state based on the "mBase" pane that holds the custom view? I'm quite lost here and have been trying to find a solution on the past couple of days without any success. I've also been searching for a similar solution (for example an unmaskable password field wich is the solution that most resembles what I'm trying to do) on the forum from where I could learn how to achieve this without any success at all. Any pointer in the right direction will be very much appreciated.

Thanks in advance for your feedback.
Best regards to all.
 
Solution
I found a solution for this, so I'm posting it here for anyone who may find it useful.

From the FocusChanged event (that includes some other unrelated code for this purpose) of the textfields within the Custom View class code I call the following Sub:

B4X:
Private Sub TestFocus()
    
    Dim boolean_Focus As Boolean = False
    For Each n As Node In mBase.GetAllViewsRecursive
        Dim jo_n As JavaObject = n
        If jo_n.RunMethodJo("focusedProperty",Null).RunMethod("get",Null) Then
            boolean_Focus = True
            Exit
        End If
    Next
    
    If Not(boolean_Focus) Then
        If SubExists(mCallBack,mEventName & "_ExitedView") Then
            CallSub(mCallBack, mEventName & "_ExitedView")
        End If...

Fernando Solá

Member
Licensed User
Longtime User
I found a solution for this, so I'm posting it here for anyone who may find it useful.

From the FocusChanged event (that includes some other unrelated code for this purpose) of the textfields within the Custom View class code I call the following Sub:

B4X:
Private Sub TestFocus()
    
    Dim boolean_Focus As Boolean = False
    For Each n As Node In mBase.GetAllViewsRecursive
        Dim jo_n As JavaObject = n
        If jo_n.RunMethodJo("focusedProperty",Null).RunMethod("get",Null) Then
            boolean_Focus = True
            Exit
        End If
    Next
    
    If Not(boolean_Focus) Then
        If SubExists(mCallBack,mEventName & "_ExitedView") Then
            CallSub(mCallBack, mEventName & "_ExitedView")
        End If
    End If
        
End Sub

I'm pretty sure it can be optimized, but at this time I don't have too much time to fiddle with it. But for the moment it helps me know when the focus leaves the Custom View to do some other stuff.
 
Upvote 0
Solution
Cookies are required to use this site. You must accept them to continue using the site. Learn more…