Supplementing With a main.xml File...

sconcequence

Member
Licensed User
Longtime User
I see solutions to the ScrollView and EditText focus problems on the Web, but they involve editing the main.xml file (I believe). The question is, how do I supplement my project with a XML file?

Also, can you inherit classes and override methods with Basic4Android?

Thanks.

--
 

sconcequence

Member
Licensed User
Longtime User
Upvote 0

sconcequence

Member
Licensed User
Longtime User
Upload...

I would be glad to, but I don't want to make it public. Is there a place I can upload it to privately? I don't want the code to be distributed.

--
 
Upvote 0

sconcequence

Member
Licensed User
Longtime User
Creating a Project Involved...

I don't have the time to create a new project like that just for example purposes right now.

What you need is a layout that goes beyond the resolution of the Layout consisting of a series of EditTexts all attached to a ScrollView.

Attached are some screenshots of what happens.

When the application first loads, nothing has the focus. But after I scroll down all of the way, The first or second EditText within view automatically gets the focus. This is unacceptable to me, because I set the .Text property of EditTexts to a null string upon focus. And if there are values filled in by the program that I want the user to view, and the EditText automatically gets focus without user intervention, then the data in that EditText is lost. In the picture, the second EditText from the top receives the focus, and no, I did not click in that EditText.

This behavior can also be seen in the Android 'Edit Contact' screen in the default Phone 'Make a Call' app. If you create enough EditTexts on screen with the '+' button and scroll down, various EditTexts within view of the top of the window will automatically receive focus after scrolling down with your finger. And it is not the finger action that is causing the change in focus, it is the scrolling itself.

It is very obvious. Hopefully this will help you see it. If not, let me know, and I will create a sample app.
 

Attachments

  • No Focus to Start.jpg
    No Focus to Start.jpg
    9.3 KB · Views: 188
  • Focus After Scroll.jpg
    Focus After Scroll.jpg
    8.8 KB · Views: 169
Last edited:
Upvote 0

sconcequence

Member
Licensed User
Longtime User
I apologize for any misunderstanding..

I apologize for any misunderstanding. It occurred to me that I chose the wrong word to express my reluctance. Please don't hold this against me. I really do need the help.

--
 
Upvote 0

sconcequence

Member
Licensed User
Longtime User
Example Project...

Ok, sorry for all of the fuss, but I have created a project that demonstrates the problem.

You will see values inside all of the EditTexts. If you are in vertical orientation mode, and scroll down, you will notice that EditText3 steals the focus and erases the value in there. This is simulating how I create values with the button and want them to stay there until the user clicks in the EditText, rather than having it get focus automatically.

In horizontal orientation mode, the focus gets stolen by both EditText5 and EditText3.

I need Android to avoid putting focus on these EditTexts as the ScrollView is scrolled.

As in the links posted before, some users have suggested ways to stop this from happening, most involving Java. Perhaps a library could be built onto B4A to incorporate these.

I am using this on Android 2.2.2

--
 

Attachments

  • editText Focus Example.zip
    7.6 KB · Views: 178
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
There are probably several possible approaches.
You can use a timer to decide when to erase the current text and when to keep it.

Check the following code:
Note that I used the Abstract Designer to set the EventName of all the EditTexts to EditText.
Your code can be much shorter. I recommend you to read the "array of views" tutorial: Basic4android Search: Array Of Views
B4X:
'Activity module
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.
   Dim value1 As String
   Dim value2 As String
   Dim value3 As String
   Dim value4 As String
   Dim value5 As String
   Dim value6 As String
   Dim value7 As String
   Dim timer1 As Timer
   Dim disableCounter As Int
End Sub

Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.
   Dim ScrollView1 As ScrollView
   Dim seperator As Canvas
   Dim p As Phone
   Dim layoutVal As LayoutValues
   Dim convertButton As Button
   Dim dummyEdit As EditText
   Dim editText1 As EditText
   Dim editText2 As EditText
   Dim editText3 As EditText
   Dim editText4 As EditText
   Dim editText5 As EditText
   Dim editText6 As EditText
   Dim editText7 As EditText
   Dim label1 As Label
   Dim label2 As Label
   Dim label3 As Label
   Dim label4 As Label
   Dim label5 As Label
   Dim label6 As Label
   Dim label7 As Label
   Dim labelTitle As Label
End Sub

Sub Activity_Create(FirstTime As Boolean)
   If FirstTime Then
      timer1.Initialize("Timer1", 100)
   End If
   Dim y1, y2 As Float
   Dim scrollViewHeight As Int
   
   layoutVal = GetDeviceLayoutValues
   
   If layoutVal.Width >= 480 Then
      scrollViewHeight = 600
   Else If layoutVal.Width >= 320 Then
      scrollViewHeight = 550
   End If

   ScrollView1.Initialize2(DipToCurrent(scrollViewHeight), "ScrollView1")
   ScrollView1.Panel.Width = 100%x
   ScrollView1.Panel.Height = DipToCurrent(scrollViewHeight)
   ScrollView1.Panel.LoadLayout("Main")
   Activity.AddView(ScrollView1, 0, 0, 100%x, 30%y)
   seperator.Initialize(ScrollView1.Panel)
   
   editText1.ForceDoneButton = True
   editText2.ForceDoneButton = True
   editText3.ForceDoneButton = True
   editText4.ForceDoneButton = True
   editText5.ForceDoneButton = True
   editText5.ForceDoneButton = True
   editText6.ForceDoneButton = True
   
   If FirstTime = True Then
      value1 = "1"
      value2 = "2"
      value3 = "3"
      value4 = "4"
      value5 = "5"
      value6 = "6"
      value7 = "7"
   End If

End Sub

Sub Activity_Resume
   Dim x1, x2, y1 As Float
   Dim layoutVal As LayoutValues
   Dim scrollViewHeight As Int
   Dim testPanel As Panel
   
   layoutVal = GetDeviceLayoutValues
      
   If layoutVal.Width >= 480 Then
      scrollViewHeight = 600
   Else If layoutVal.Width >= 320 Then
      scrollViewHeight = 550
   End If

   ScrollView1.Panel.Height = DipToCurrent(scrollViewHeight)
   x1 = 0
   x2 = 100%x
   y1 = (editText5.Top + editText4.Top + editText4.Height) / 2
   seperator.DrawLine(x1, y1, x2, y1, Colors.White, 1)

   editText1.ForceDoneButton = True
   editText2.ForceDoneButton = True
   editText3.ForceDoneButton = True
   editText4.ForceDoneButton = True
   editText5.ForceDoneButton = True
   editText5.ForceDoneButton = True
   editText6.ForceDoneButton = True

   editText1.Text = value1
   editText2.Text = value2
   editText3.Text = value3
   editText4.Text = value4
   editText5.Text = value5
   editText6.Text = value6
   editText7.Text = value7

End Sub

Sub Activity_Pause (UserClosed As Boolean)
value1 = editText1.Text
value2 = editText2.Text
value3 = editText3.Text
value4 = editText4.Text
value5 = editText5.Text
value6 = editText6.Text
value7 = editText7.Text
End Sub

Sub EditText_FocusChanged (HasFocus As Boolean)
   If HasFocus = True Then
      Dim e As EditText
      e = Sender
      If timer1.Enabled = False Then
         e.Text = ""
      End If
   End If
End Sub

Sub ScrollView1_ScrollChanged(Position As Int)
   disableCounter = 3
   If timer1.Enabled = False Then timer1.Enabled = True
End Sub
Sub Timer1_Tick
   disableCounter = disableCounter - 1
   If disableCounter <= 0 Then timer1.Enabled = False
End Sub

You can use Phone.HideKeyboard to prevent the keyboard from showing when the timer is enabled.
 
Upvote 0

sconcequence

Member
Licensed User
Longtime User
Timer Solution...

The problem with the timer solution is that someone might start typing before the timer has run out, and then the text will be removed from the EditText if they scroll, then click inside the EditText shortly thereafter.

What about your other suggestions? Also, I think it would be a good idea to include the solutions in the links for future versions of B4A. You don't seem to think so. Why not?

--
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
The timer interval is too large. You can shorten it significantly.

The links you posted seem to use two methods. The first is to steal the focus with an invisible view. There is no need to create an XML file for this.
The second is to disable the EditText while the user is scrolling. I think that it can already be implemented.
 
Upvote 0
Top