Android Question [Solved] where is focus after activity-create? Set that view per code?

Pflichtfeld

Active Member
Licensed User
Which view has the focus after activity-create?
Can I set this focus per code?

(background: I want to remove focus from edittext after Enter pressed. The Reflector solution removes the ability to get focus, not acceptable. Did not test phone hidekeyboard , as the code is in a class. To use an Edittext outside the screen is a lousy solution imho. Try to set focus to the parent panel also fails. Isn't there a neat/fair solution?)
 

LucaMs

Expert
Licensed User
Longtime User
Which view has the focus after activity-create?
On the first focusable one. First in your layout (see the "Views Tree").

Can I set this focus per code?
EditText1.RequestFocus

Try to set focus to the parent panel also fails.
Set the Panel as "focusable".

B4X:
Sub Activity_Create
    Activity.Loadlayout("xxx")
    SetFocusable(Panel1, True)
    EditText1.RequestFocus
' '''
End Sub

B4X:
Sub EditText1_EnterPressed
    RequestFocus(Panel1)
End Sub

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

Sub RequestFocus(Vw As View)
    Dim jo As JavaObject = Vw
    jo.RunMethod("requestFocus", Null)
End Sub
 
Last edited:
Upvote 0

Pflichtfeld

Active Member
Licensed User
Thank you, Lucas, that is nearly perfect!

1. is seems that
B4X:
Sub EditText1_EnterPressed
    Panel1.RequestFocus
End Sub
also works. Is that correct?

2. The focus is lost. Fine! But the keyboard is still opened. Can I force it to close as well?
 
Upvote 0
Top