Android Question <Resolved>Are there any other events that can be accessed from the edit text view?

DataMiser

Member
Licensed User
I am still running into some issues trying to get this android app to behave the way it needs to. It appears that the only events fired are focus changed, text changed and enter pressed and enter seems to automatically send focus to the next control in the list. This auto focus change is a bit of an issue. I have sort of found a way around it but still not sure it works 100%.

A problem arises when the user wants to tap on a different field for whatever reason. I need to be able to allow the user to do this even when there is not a valid entry in the currently active field, yet I need to keep the app from going to the next field when enter is pressed and no valid entry is present in the current field.

So that said is there anyway to tell when a user taps on an edittext view? Are there any other possible events that may be raised from an edittext view other than the three shown in the designer?

I really like this B4A tool but find it very limited event wise compared to what I am used to. Trying to reproduce this old Windows CE app has proven difficult and of all things the thing that has proven the most difficult has been simply getting the focus to go and stay where it needs to be. In VB this is very simple to do but apparently not so much on the Android or if there is a simple way I have not been able to find it yet.

I guess the real issue here in trying to get this to behave the way I need is that the auto focus change happens after the code in the enter pressed event runs. If I place code in the enter pressed to request focus on any field then the focus goes there then goes to the next field in the list. The focus changed event fires after that so it can be used to override the focus but can not tell if a user tapped on the field or not.
 

Jeffrey Cameron

Well-Known Member
Licensed User
Longtime User
Is there a reason you're not just handling the _EnterPressed event in the text box and ignoring everything else?

For my apps, that is all I handle, I don't worry about focus change during entry and if they hit Enter then I'll look at what's in the box, if it's ok I'll RequestFocus on the "next" field or throw a warning if invalid input. If I need to validate the entire screen I'll do so in the "next" or "all done" button event.

Bear in mind there's a reason it's called RequestFocus and not SetFocus. There are times when the OS may decide that the field you want to have focus isn't the one it wants to have in focus, so it will ignore your request.

Android behaves quite differently than WinCE or Windows in general. For some things you may not be able to emulate the Windows behavior exactly simply because Android doesn't/can't work that way.
 
Upvote 0

William Lancee

Well-Known Member
Licensed User
Longtime User
When view A is pressed while B has focus then B will lose focus without having an EnterPressed event. You can use that fact to distinguish between the two situations.
 
Upvote 0

DataMiser

Member
Licensed User
Is there a reason you're not just handling the _EnterPressed event in the text box and ignoring everything else?
That is of course exactly what I do on the Windows CE device and the first thing I tried but it does not give the expected results on the Android

Code I have in the enter pressed event checks the data entered, determines if it is ok to proceed. So far so good.
If I tell it to go to the next field it does, then it goes to the next field after that. So it effectively skips a field.
If I do not tell it to go to the next field it goes to the next field if there is one lower on the list otherwise it goes to one of the buttons at the bottom.
In the case of the last field I need it to go to a different field, which varies. It may be the first field it needs to go to, or it may need to go to the 3rd field or wherever but if I tell it to go there in the enter pressed event it goes there then it goes to the next field after that so again skipping a field.

In the other case if there is an invalid entry and I want to notify the user and have the re enter the info I request focus but the focus still goes to the next field or button.

This behavior may be in part because I am using a hard keyboard and barcode scanner for data entry but those are both requirements for the app.

At any rate I need to be able to control which field gets focus after an enter keypress and still allow the user to move focus by tapping on a field of their choice. I've tried several different things already but so far none of them have worked reliably to do what I need it to do.
 
Upvote 0

William Lancee

Well-Known Member
Licensed User
Longtime User
If there is no EnterPressed event then normal response is to give focus to the touched target view, and you can handle FocusChanged event for that view if HasFocus = True.

B4X:
Private Sub EditText1_EnterPressed
    Log("1 enter pressed")
    trackEnterPressed = True            'trackEnterPressed is a process or class global
End Sub

Private Sub EditText1_FocusChanged (HasFocus As Boolean)        'occurs immediately after EnterPressed
    Log("1 focus changed " & HasFocus)
    If HasFocus = False Then
        If trackEnterPressed Then
            trackEnterPressed = False
            'check validity
            Dim valid As Boolean = False        'or False
            If valid Then
                EditText3.RequestFocus         'override next view EditText2 in this test case
            Else
                xui.MsgboxAsync("This entry needs ...", "Problem")
                EditText1.RequestFocus
            End If
        End If
    End If
End Sub
 
Upvote 0

DataMiser

Member
Licensed User
Another thought. Is it possible to programmatically set the next field in sequence?
What appears to be happening is that when the Enter Key is pressed the enter pressed event fires, executes any code that is there and then focus shifts to the next field in sequence. If you request focus to a different field within the enter keypressed then that field gets focus while still in that enter pressed event and after the event the focus changes to the next field in sequence. It seems that if there was a way to tell it what the next field should be it would solve all the issues I have been running into.
 
Upvote 0

DataMiser

Member
Licensed User
If there is no EnterPressed event then normal response is to give focus to the touched target view, and you can handle FocusChanged event for that view if HasFocus = True.

B4X:
Private Sub EditText1_EnterPressed
    Log("1 enter pressed")
    trackEnterPressed = True            'trackEnterPressed is a process or class global
End Sub

Private Sub EditText1_FocusChanged (HasFocus As Boolean)        'occurs immediately after EnterPressed
    Log("1 focus changed " & HasFocus)
    If HasFocus = False Then
        If trackEnterPressed Then
            trackEnterPressed = False
            'check validity
            Dim valid As Boolean = False        'or False
            If valid Then
                EditText3.RequestFocus         'override next view EditText2 in this test case
            Else
                xui.MsgboxAsync("This entry needs ...", "Problem")
                EditText1.RequestFocus
            End If
        End If
    End If
End Sub
Thanks,
I'll give that a try and see how it goes. I had been working with the focus changed event with limited success but have not tried it quite like that yet.
 
Upvote 0

William Lancee

Well-Known Member
Licensed User
Longtime User
Addendum.

Using above, the enter press will give (and immediate take away focus) from the next view. Therefore that view, say EditText2, must be able to ignore that focusChanged to True. One way is to sleep(0) and check if focus is still there.

The following works, but there may be better ways.

B4X:
Private Sub EditText2_FocusChanged (HasFocus As Boolean)
    Log("2 focus changed " & HasFocus)
    If HasFocus Then
        Sleep(0)
        Log("Focus cancelled " & focusCancelled)            'focusCancelled is global
        If focusCancelled = False Then
            'handle user view choice
        Else
            focusCancelled = False
        End If
    Else
        focusCancelled = True
    End If
End Sub
 
Upvote 0

DataMiser

Member
Licensed User
I modified one of my layouts using code like show in post #6 and ran a few tests. It appears to be doing what I need it to do. It appears I was on the right track trying to use the focus changed event but was still a few head bangs on the wall from getting there ;)

Thanks saved me a lot of time and headache. I think I can finish the app now and move on to testing.
 
Upvote 0

DataMiser

Member
Licensed User
So after I made the changes to the code for one of the layouts I did several tests and all seems well at this point.
This is a sample of what I am using now.
B4X:
Private Sub txtPart_EnterPressed
    EnterUsed=True
End Sub

Private Sub txtPart_FocusChanged (HasFocus As Boolean)
    If HasFocus=False Then
        If EnterUsed Then
            EnterUsed=False
            If txtPart.Text.Length<2 Then
                ModCommon.soundbad
                MsgboxAsync("Entry of at least 2 characters required", "Entry Error")
                txtPart.requestfocus
            Else
                ModCommon.soundgood
                txtQty.requestfocus
            End If
        End If
    Else
        If EnterUsed=False Then
            txtPart.SelectAll
        End If
    End If
End Sub

After running through different scenarios about 100 times I saw only one glitch where it started on the wrong field but was not able to reproduce it. Confident that glitch was from something outside the changed code.

Thanks again for the assist ?
 
Upvote 0
Top