Android Question Error when trying to set focus to an edittext in a clv

tsteward

Well-Known Member
Licensed User
Longtime User
I have a CLV and each row has a label as view(0) and an edittext as view(1)
When I use the following code to change focus from one edittext in one row to the edittext in the next row I get the error in the image setSpan(1 ... 1) ends beyond length 0

How do I fix this please

B4X:
currentcut = currentcut + 1
Dim pa As Panel = clvSideA.GetPanel(currentcut)
pa.GetView(1).RequestFocus
 

Attachments

  • Screenshot 2023-11-04 203750.png
    Screenshot 2023-11-04 203750.png
    22.6 KB · Views: 45

mangojack

Well-Known Member
Licensed User
Longtime User
I created a project with 20 row CLV.

Using a Button , your above code worked correctly , placing focus on the next EditText .

B4X:
Sub Button1_Click
    currentcut = currentcut + 1
    Dim pa As Panel = clvSideA.GetPanel(currentcut)
    pa.GetView(1).RequestFocus
End Sub

How / when are you running this code. ? This error is more in line with a .SelctionStart or .SelectionLenth call.
 
Last edited:
Upvote 0

tsteward

Well-Known Member
Licensed User
Longtime User
I'm using SD Custom keyboard and suspect it might be too do with that.
When a button is pressed on the custom keyboard it enters the text into the edittext. The text changed event fires and this is where I set focus to the next exittext.
 
Upvote 0

mangojack

Well-Known Member
Licensed User
Longtime User
Upvote 0

tsteward

Well-Known Member
Licensed User
Longtime User
here is a test app showing the error
 

Attachments

  • TS_Test.zip
    13.2 KB · Views: 30
Upvote 0

Mariano Ismael Castro

Active Member
Licensed User
here is a test app showing the error
Try this, I hope I understood what you wanted to do.
Private Sub ETCutA_TextChanged (Old As String, New As String):
Private Sub ETCutA_TextChanged (Old As String, New As String)
    Dim ETCutAView As Object = Sender
    If ETCutAView Is EditText Then
        Dim position2 As Int = clv.GetItemFromView(ETCutAView)       
        If position2 < clv.Size - 1 Then 'So that it does not crash when it is in the last EditText
            Dim nextPanel As Panel = clv.GetPanel(position2 + 1)
            Dim nextEditText As EditText = nextPanel.GetView(1)
            Sleep(10) 'this prevents the following error -> java.lang.IndexOutOfBoundsException: setSpan
            nextEditText.RequestFocus
        End If   
    End If
End Sub
 
Upvote 0

tsteward

Well-Known Member
Licensed User
Longtime User
Thanks Heaps
 
Upvote 0

mangojack

Well-Known Member
Licensed User
Longtime User
here is a test app showing the error

As a side note ... be aware that ETCutA_TextChanged event fires twice on each keypress.

This also works , but I think @Mariano Ismael Castro approach is better.

B4X:
Private Sub ETCutA_TextChanged (Old As String, New As String)

    Log("ETCutA Text Changed  Position = " & position)
   
    If flagTextChanged = False Then    'global flag
        flagTextChanged = True
        Return
    End If
   
    position = position+1
    If position < clv.Size Then
        Dim pa As Panel = clv.GetPanel(position)
        Sleep(0)        'required
        pa.GetView(1).RequestFocus
        flagTextChanged = False
    End If
   
End Sub
 
Upvote 0

tsteward

Well-Known Member
Licensed User
Longtime User
Hey guys much smarter than me thanks for your help so far but could I trouble you a little further.
At the moment I have it so that you click the keyboard the text is entered and it moves onto the next field selecting all text if any so pressing the keyboard replaces all existing text.
This is exactly what I want

I need help so that when a user manually selects another edittex it automatically selects all text but can't figure it out.

Thank you
 

Attachments

  • TS_Test.zip
    13.4 KB · Views: 42
Upvote 0

mangojack

Well-Known Member
Licensed User
Longtime User
I need help so that when a user manually selects another edittex it automatically selects all text but can't figure it out.

I had a play with the example ... Unfortunately I could not resolve your issue & I am now out of time (back to work for me ..)

A couple of observations though.
B4X:
Private Sub ETCuta_FocusChanged (HasFocus As Boolean)

Also the code never executes past testing the view as an EditText , (which ETCutA is )

B4X:
Private Sub ETCutA_FocusChanged (HasFocus As Boolean)
    If HasFocus = True Then
        Dim ETCutAView As Object = Sender
        
        If ETCutAView Is EditText Then        'commenting this line test results in the following error
            
            'java.lang.ClassCastException: b4a.example.sd_keyboard cannot be cast to android.view.View
            
            Dim position2 As Int = clv.GetItemFromView(ETCutAView)
            Dim nextPanel As Panel = clv.GetPanel(position2)
            Dim nextEditText As EditText = nextPanel.GetView(1)
            Sleep(10) 'this prevents the following error -> java.lang.IndexOutOfBoundsException: setSpan
            nextEditText.SelectAll
        End If
    End If
End Sub

Without having any knowledge working with the SD Custom Keyboard , again I cannot help you with this one.

Cheers Ian
 
Upvote 0

tsteward

Well-Known Member
Licensed User
Longtime User
Although you tap on the edittext the view returned is the SD Custom keyboard.
It doesn't even for the clv clicked event

Thanks anyway
 
Upvote 0

tsteward

Well-Known Member
Licensed User
Longtime User
I have tried putting a transparent panel over the edittext and when clicked it returns the panel but my code still will not select all text.
I don't actually want the user to type into the edittext but replace it with what's on the keyboard when pressed.

Trapping the panel touch does not seem to work either.
 

Attachments

  • TS_Test.zip
    13.7 KB · Views: 37
Upvote 0

Mariano Ismael Castro

Active Member
Licensed User
I have tried putting a transparent panel over the edittext and when clicked it returns the panel but my code still will not select all text.
I don't actually want the user to type into the edittext but replace it with what's on the keyboard when pressed.

Trapping the panel touch does not seem to work either.
Try this

Panel1_Touch:
Private Sub Panel1_Touch (Action As Int, X As Float, Y As Float)
    Dim pan As Object = Sender
    If pan Is Panel Then
        Dim position2 As Int = clv.GetItemFromView(pan)
        Dim panItem As Panel = clv.GetPanel(position2)
        Dim txtETCutA As EditText = panItem.GetView(1)
        Sleep(10) 'this prevents the following error -> java.lang.IndexOutOfBoundsException: setSpan
        txtETCutA.SelectAll
        txtETCutA.RequestFocus
    End If   
End Sub
 
Upvote 1

tsteward

Well-Known Member
Licensed User
Longtime User
Try this

Panel1_Touch:
Private Sub Panel1_Touch (Action As Int, X As Float, Y As Float)
    Dim pan As Object = Sender
    If pan Is Panel Then
        Dim position2 As Int = clv.GetItemFromView(pan)
        Dim panItem As Panel = clv.GetPanel(position2)
        Dim txtETCutA As EditText = panItem.GetView(1)
        Sleep(10) 'this prevents the following error -> java.lang.IndexOutOfBoundsException: setSpan
        txtETCutA.SelectAll
        txtETCutA.RequestFocus
    End If 
End Sub
Perfect thank you exactly what I was trying to do.
I was close but no cigar :)
 
Upvote 0

MrKim

Well-Known Member
Licensed User
Longtime User
I had an issue with this in B4i. I have my own onscreen keyboard and it turns out ios won't select text unless the keyboard is visible.
So I gave up trying to highlight and just replace the text.
 
Upvote 0
Top