I am using the Searchview library and have created an edit text box in a seperate activity. When I run my search activity using the searchview code, I can't get the box to become focused, as I want the keyboard to come up every single time the user hits the search button.
What happens right now is that the very first time the activity is called (first time you press the search button) the keyboard will auto open, but after that, nothing.
Search activity:
And the modified searchview code:
What happens right now is that the very first time the activity is called (first time you press the search button) the keyboard will auto open, but after that, nothing.
B4X:
Sub Search_Click
StartActivity(Search)
End Sub
Search activity:
B4X:
#Region Activity Attributes
#FullScreen: False
#IncludeTitle: false
#End Region
Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
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 AD As ActionDrawer
Dim sv As SearchView
Dim index As Object
Dim keyboard As IME
End Sub
Sub Activity_Create(FirstTime As Boolean)
'Do not forget to load the layout file created with the visual designer. For example:
'Activity.LoadLayout("Layout1")
'load the searchview function
sv.Initialize(Me,"sv")
'If FirstTime Then
Dim cities As List
cities = File.ReadList(File.DirAssets, "Cities.txt")
' As an optimization we store the index As a process global variable
' AND only build it once.
index = sv.SetItems(cities)
'Else
'sv.SetIndex(index)
'End If
Dim Overflow3 As List
Overflow3.Initialize
Overflow3.Add("Source")
AD.Initialize("", "ic_icon.png", True, Colors.RGB(213,213,213), "light", False, True, False, Overflow3, "Up", Activity, Me)
' 'If you want to use a light theme add "SetApplicationAttribute(android:theme, "@android:style/Theme.Holo.Light")" to the manifest
'
AD.AppTitle = ""
sv.AddToParent(Activity, 10dip, 0, 70%x, -10%y,0,50dip, 100%x, 90%y)
keyboard.ShowKeyboard(sv.et)
sv.et.RequestFocus
End Sub
Sub sv_ItemClick(Value As String)
keyboard.HideKeyboard
Dim searchflag As Int
searchflag = 1
Main.countrytypesearch = Value
Main.searchedflag = searchflag
StartActivity(Main)
End Sub
Sub up_click
End Sub
Sub Activity_Resume
sv.et.Text = ("")
sv.et.RequestFocus
CallSub(sv, "Focus")
End Sub
Sub Activity_Pause (UserClosed As Boolean)
sv.et.Text = ("")
sv.et.RequestFocus
End Sub
Sub Activity_KeyPress (KeyCode As Int) As Boolean
If KeyCode = KeyCodes.KEYCODE_BACK Then
StartActivity(Main) ' Hardware-Zurück Taste gedrückt
Return True ' Hardware-Zurück Taste deaktiviert
Else
Return False
End If
End Sub
And the modified searchview code:
B4X:
'version 1.00
'Class module
Sub Class_Globals
Private prefixList As Map
Private substringList As Map
Public et As EditText
Private lv As ListView
Private MIN_LIMIT, MAX_LIMIT As Int
MIN_LIMIT = 1
MAX_LIMIT = 4 'doesn't limit the words length. Only the index.
Private mCallback As Object
Private mEventName As String
End Sub
'Initializes the object and sets the module and sub that will handle the ItemClick event
Public Sub Initialize (Callback As Object, EventName As String)
et.Initialize("et")
'Remove the suggestions bar
et.InputType = Bit.OR(et.INPUT_TYPE_TEXT, 0x00080000)
lv.Initialize("lv")
lv.SingleLineLayout.ItemHeight = 50dip
lv.SingleLineLayout.Label.TextSize = 18
lv.Visible = False
et.TextColor = Colors.White
et.HintColor = Colors.ARGB(80,255,255,255)
et.Hint = "Enter name of the country"
lv.SingleLineLayout.Label.TextColor = Colors.Black
prefixList.Initialize
substringList.Initialize
mCallback = Callback
mEventName = EventName
et.RequestFocus
End Sub
'Adds the view to the parent. The parent can be an Activity or Panel.
Public Sub AddToParent(Parent As Panel, Left As Int, Top As Int, Width As Int, height As Int, left2 As Int, top2 As Int, width2 As Int, height2 As Int)
Parent.AddView(et, 50dip, -1dip, 265dip, 50dip)
Parent.AddView(lv, left2, top2, width2, height2)
SetNinePatchButton(et, "apptheme_textfield_activated_holo_light_thin", "apptheme_textfield_activated_holo_light_thin")
End Sub
Sub SetNinePatchButton(Btn As EditText, DefaultImage As String, PressedImage As String)
Dim r As Reflector
Dim package As String
Dim idDefault, idPressed As Int
package = r.GetStaticField("anywheresoftware.b4a.BA", "packageName")
idDefault = r.GetStaticField(package & ".R$drawable", DefaultImage)
idPressed = r.GetStaticField(package & ".R$drawable", PressedImage)
r.Target = r.GetContext
r.Target = r.RunMethod("getResources")
Dim sd As StateListDrawable
sd.Initialize
sd.AddState(sd.State_Pressed, r.RunMethod2("getDrawable", idPressed, "java.lang.int"))
sd.AddCatchAllState( r.RunMethod2("getDrawable", idDefault, "java.lang.int"))
Btn.Background = sd
End Sub
Private Sub lv_ItemClick (Position As Int, Value As Object)
et.Text = Value
et.SelectionStart = et.Text.Length
lv.Visible = False
If SubExists(mCallback, mEventName & "_ItemClick") Then
CallSub2(mCallback, mEventName & "_ItemClick", Value)
End If
End Sub
Private Sub et_TextChanged (Old As String, New As String)
lv.Clear
If lv.Visible = False Then lv.Visible = True
If New.Length < MIN_LIMIT Then Return
Dim str1, str2 As String
str1 = New.ToLowerCase
If str1.Length > MAX_LIMIT Then
str2 = str1.SubString2(0, MAX_LIMIT)
Else
str2 = str1
End If
AddItemsToList(prefixList.Get(str2), str1)
AddItemsToList(substringList.Get(str2), str1)
et.RequestFocus
End Sub
Private Sub AddItemsToList(li As List, full As String)
If li.IsInitialized = False Then Return
For i = 0 To li.Size - 1
Dim item As String
item = li.Get(i)
If full.Length > MAX_LIMIT AND item.ToLowerCase.Contains(full) = False Then
Continue
End If
lv.AddSingleLine(li.Get(i))
Next
End Sub
'Builds the index and returns an object which you can store as a process global variable
'in order to avoid rebuilding the index when the device orientation changes.
Public Sub SetItems(Items As List) As Object
Dim startTime As Long
startTime = DateTime.Now
'ProgressDialogShow2("Building index...", False)
Dim noDuplicates As Map
noDuplicates.Initialize
'prefixList.Clear
'substringList.Clear
Dim m As Map
Dim li As List
For i = 0 To Items.Size - 1
If i Mod 100 = 0 Then DoEvents
Dim item As String
item = Items.Get(i)
item = item.ToLowerCase
noDuplicates.Clear
For start = 0 To item.Length
Dim count As Int
count = MIN_LIMIT
Do While count <= MAX_LIMIT AND start + count <= item.Length
Dim str As String
str = item.SubString2(start, start + count)
If noDuplicates.ContainsKey(str) = False Then
noDuplicates.Put(str, "")
If start = 0 Then m = prefixList Else m = substringList
li = m.Get(str)
If li.IsInitialized = False Then
li.Initialize
m.Put(str, li)
End If
li.Add(Items.Get(i)) 'Preserve the original case
End If
count = count + 1
Loop
Next
Next
ProgressDialogHide
Log("Index time: " & (DateTime.Now - startTime) & " ms (" & Items.Size & " Items)")
Return Array As Object(prefixList, substringList)
End Sub
'Sets the index from the previously stored index.
Public Sub SetIndex(Index As Object)
Dim obj() As Object
obj = Index
prefixList = obj(0)
substringList = obj(1)
End Sub
Public Sub focus
Dim keyboard As IME
keyboard.ShowKeyboard(et)
et.RequestFocus
End Sub