Android Question Prevent auto-focus on edittext while scrolling a scrollview

rbw152

Member
Licensed User
Longtime User
I have a scroll view with a Label, Edittext, 3 buttons, a checkbox and a spinner all dynamically generated on each row (many rows are made). When I scroll the scrollview, one of the edittexts gets focus, even if my finger is scrolling over the other controls.

Any ideas as to why this is happening and a way to stop it? I'd like to have focus of these edittexts at some point, but only when I actually click on them while the scrollview is stationary.

Thanks.
 

Harris

Expert
Licensed User
Longtime User
Generally, this is where I use a _longclick event handler. However, I don't know if you can apply this to an edittext. Interesting...
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Can you create a small project that demonstrates this?

I've tried it with this code:
B4X:
Sub Globals

   Private ScrollView1 As ScrollView
End Sub

Sub Activity_Create(FirstTime As Boolean)
   ScrollView1.Initialize2(1000dip,  "ScrollView1")
   Activity.AddView(ScrollView1, 0, 0, 100%x, 100%y)
   Dim et As EditText
   et.Initialize("et")
   ScrollView1.Panel.AddView(et, 0, 0, 200dip, 100dip)
End Sub
 
Upvote 0

rbw152

Member
Licensed User
Longtime User
Can you create a small project that demonstrates this?

I've tried it with this code:
B4X:
Sub Globals

   Private ScrollView1 As ScrollView
End Sub

Sub Activity_Create(FirstTime As Boolean)
   ScrollView1.Initialize2(1000dip,  "ScrollView1")
   Activity.AddView(ScrollView1, 0, 0, 100%x, 100%y)
   Dim et As EditText
   et.Initialize("et")
   ScrollView1.Panel.AddView(et, 0, 0, 200dip, 100dip)
End Sub

I ran that code you posted, Erel. Quite rightly, it worked flawlessly. However, I noticed (and I should have mentioned this before actually) that changing the scrollview to scrollview2d does the auto-focusing. Perhaps i'm using scrollview2d wrong? Try changing it to see what i'm talking about.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Tested with this code:
B4X:
Sub Globals
  Private ScrollView1 As ScrollView2D
End Sub

Sub Activity_Create(FirstTime As Boolean)
  ScrollView1.Initialize(500dip,  1000dip,  "ScrollView1")
  Activity.AddView(ScrollView1, 0, 0, 100%x, 100%y)
  Dim et As EditText
  et.Initialize("et")
  ScrollView1.Panel.AddView(et, 0, 0, 200dip, 100dip)
End Sub
The keyboard didn't show when scrolling the scrollview.
 
Upvote 0

rbw152

Member
Licensed User
Longtime User
Same here. But the cursor is still thrown into the edittext for me. When I change it back to a normal scrollview, the edittext is left alone.
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
This code works OK on my xperia z1 (Android 4.4.2 B4A 3.80)
B4X:
Sub Globals
    Dim scvTest As ScrollView2D
End Sub

Sub Activity_Create(FirstTime As Boolean)
    scvTest.Initialize(100, 100, "")
    Activity.AddView(scvTest, 0, 0, 100%x, 100%y)
 
    Dim i, j As Int
 
    For i = 0 To 20
        For j = 0 To 4
            Dim edt As EditText
            edt.Initialize("edt")
            scvTest.Panel.AddView(edt, 5dip + j * 155dip, 5dip + i * 55dip, 150dip, 50dip)
            edt.Text = i * 5 + j
        Next
    Next
    scvTest.Panel.Width = 5dip + 4 * 155dip
    scvTest.Panel.Height = 5dip + 21 * 55dip
End Sub
Could you post your project or at least a small one showing the problem.
 

Attachments

  • ScrollView2D_EditText.zip
    6.1 KB · Views: 307
Upvote 0

rbw152

Member
Licensed User
Longtime User
Thanks for your reply. I'm afraid that all I would be doing would be posting back the same code you've just provided.

If I swish my finger along the scrollview2d in that code you posted, one of the numbered boxes automatically gets the focus (not necessarily the one my finger touches first either). I have noticed (the code you just posted highlights this very well actually) that the box that ends up with the focus is usually one of the ones that ends up being in one of the corners of the currently visible screen of boxes after the scrolling animation finishes.

So just to be clear, I scroll, the scrolling animation runs, it comes to a rest, and one of the boxes (in one of the corners of the now resting screen) now has focus.

It's nuts! XD

Oh, and I updated scrollview2d library from 1.03 to 1.1. Still same problem.

Running B4A v3.50 and Android 4.0.3 (using lease device so I cant update that one, sorry).

I've started lighting candles and drawing religious symbols on my desk.
 
Upvote 0
Top