Android Question how to know if a button has focus?

omarruben

Active Member
Licensed User
Longtime User
or is there and event fired on focus for buttons or swiftbuttons?

need to interact o a screen with android box that does not have touch screen ,only keyboard
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
 
Upvote 0

Spavlyuk

Active Member
Licensed User
Alternatively, you can check if a specific button is equal to the currently focused view.
B4X:
Public Sub GetFocusedView As View
    Dim R As Reflector
    Return R.GetActivity.As(JavaObject).RunMethod("getCurrentFocus", Null)
End Sub
 
Upvote 0

omarruben

Active Member
Licensed User
Longtime User
this code works great :
B4X:
Sub SetUpOnFocusListener(Button As Button, EventName As String)
 
    Dim JO As JavaObject = Button
    Dim event As Object = JO.CreateEvent("android.view.View.OnFocusChangeListener",EventName,False)
    JO.RunMethod("setOnFocusChangeListener",Array As Object(event))
End Sub

Sub Help_Event(MethodName As String, Args() As Object)
    Dim btn As Button
    btn = Sender
 
    Log("Button has focus: " & Args(1)) 'Args(1) is True if button has focus, false otherwise.
    If Args(1)= True Then
        btn.Color = xui.Color_Blue
    Else
        btn.Color = xui.Color_DarkGray
    End If
End Sub

B4X:
''
SetUpOnFocusListener(btnTest,"Help")

BUT NOW , is it possible to use it with swiftbutton??
I did try this code, but it does not work

B4X:
Sub SetUpOnFocusListener2(Button As SwiftButton, EventName As String)
 
    Dim JO As JavaObject = Button ' <<<--- can not accept swiftbutton
    Dim event As Object = JO.CreateEvent("android.view.View.OnFocusChangeListener",EventName,False)
    JO.RunMethod("setOnFocusChangeListener",Array As Object(event))
End Sub

Sub Help2_Event(MethodName As String, Args() As Object)
    Dim btn As SwiftButton
    btn = Sender
 
    Log("Button has focus: " & Args(1)) 'Args(1) is True if button has focus, false otherwise.
    If Args(1)= True Then
        btn.mBase.Color = xui.Color_Blue
    Else
        btn.mBase.Color = xui.Color_DarkGray
    End If
End Sub
B4X:
[CODE=b4x]''

SetUpOnFocusListener2(swifButton1,"Help")
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
1. "Custom views" instances are not views by themselves.
2. SwiftButton.mBase will return the base panel.
Try to call this to make it focusable:
B4X:
Sub SetFocusable(Vw As View, Focusable As Boolean)
    Dim jo As JavaObject = Vw
    jo.RunMethod("setFocusable", Array(Focusable))
    jo.RunMethod("setFocusableInTouchMode", Array(Focusable))
End Sub
SetFocusable(Button.mBase, True)
 
Upvote 0

omarruben

Active Member
Licensed User
Longtime User
thank you so much It works!!! the combination of all posting made it work, I will post a small sample that works for future reference. (after I finish my work)

thank you everybody !!!! @Spavlyuk , @Erel and others
 
Upvote 0

omarruben

Active Member
Licensed User
Longtime User
B4X:
Sub createItem(title As String) As B4XView
    Dim p As B4XView = xui.CreatePanel("")
    p.SetLayoutAnimated(0,0,0,100%x,35dip)
    p.LoadLayout("customeListViewItem")
    LabelItem1.Text = title
    SetFocusable(p,True)
    SetUpOnFocusListener2(p,"Help2")
    
    Return p
    
End Sub

after set up the focus solution and works fine, how do I assign a click event on the row on the customelistview? can it be done programmatically?
 
Upvote 0
Top