Android Question searchview question

Discussion in 'Android Questions' started by tufanv, Oct 24, 2017.

  1. tufanv

    tufanv Expert Licensed User

    Hello

    I have added a button to end of the baseview. For the click event I used :

    Code:
    Sub lblcl_click
        lv.Visible=
    False
        et.Text=
    ""
        lblcl.Visible=
    False

    End Sub
    This closes the lv but because it doesnt remove the focus from the et , a listview still shows up as it thinks that I am about to write stg. How can i fix this , except this everything works fine. this is the code I have used as searchview module : ( also I tried to add invisible edittext and set the focus to it but it didnt work )
    Code:
    #Event: ItemClick (Value As String)
    #DesignerProperty: Key: HighlightColor, DisplayName: Highlight Color, FieldType: Color, DefaultValue: 0xFFFD5C5C
    #DesignerProperty: Key: TextColor, DisplayName: Text Color, FieldType: Color, DefaultValue: Null
    'Class module
    Sub Class_Globals
        
    Private prefixList As Map
        
    Private substringList As Map
        
    Private et As EditText
        
    Private lv As ListView
        
    Dim lblcl As Label
        
    Private MIN_LIMIT = 1, MAX_LIMIT = 4 As Int 'doesn't limit the words length. Only the index.
        Private mCallback As Object
        
    Private mEventName As String
        
    Private mBase As Panel
        
    Private ime As IME
        
    Private maxHeight As Int
        
    Private highlightColor As Int = 0xFFFD5C5C
        
    Private Const DefaultColorConstant As Int = -984833 'ignore
        Private SearchView1 As SearchView
        
    Dim txtfocusicin As EditText
    End Sub

    Public Sub Initialize (vCallback As Object, vEventName As String)
        mEventName = vEventName
        mCallback = vCallback
        et.Initialize(
    "et")
        lblcl.Initialize(
    "lblcl")
        txtfocusicin.Initialize(
    "txtfocusicin")
        txtfocusicin.Visible=
    false
        
    'Remove the suggestions bar
        et.InputType = Bit.Or(et.INPUT_TYPE_TEXT, 0x00080000)
        lv.Initialize(
    "lv")
        lv.SingleLineLayout.ItemHeight = 
    30dip
        lv.SingleLineLayout.Label.TextSize = 
    14
        lv.Visible = 
    False
        prefixList.Initialize
        substringList.Initialize
        
    ime.Initialize("")
      
        et.TextSize=
    13
        et.Gravity=
    Gravity.CENTER_HORIZONTAL
        et.Color=
    Colors.RGB(68,68,68)
        lblcl.Color=
    Colors.Red
        lblcl.Gravity=
    Gravity.CENTER
        lblcl.TextColor=
    Colors.White
        lblcl.Text=
    "Close"
        lblcl.Visible=
    False
    End Sub

    Public Sub DesignerCreateView (Base As Panel, lbl As Label, Props As Map)
        mBase = Base
        mBase.AddView(et, 
    00, mBase.Width, 30dip)
        mBase.AddView(lv, 
    0, et.Height, mBase.Width, mBase.Height - et.Height-30dip)
        mBase.AddView(lblcl, 
    0, mBase.Height-30dip, mBase.Width, 30dip)
        maxHeight = mBase.Height
        highlightColor = Props.Get(
    "HighlightColor")
        
    Dim c As Int = Props.Get("TextColor")
        
    If c <> DefaultColorConstant Then
            lv.SingleLineLayout.Label.TextColor = c
            et.TextColor = c
        
    End If
    End Sub

    Public Sub ActivityHeightChanged(NewHeight As Int)
        
    Dim baseHeight As Int = Min(maxHeight, NewHeight - mBase.Top)
        mBase.SetLayout(mBase.Left, mBase.Top, mBase.Width, baseHeight)
        lv.SetLayout(
    0, lv.Top, lv.Width, baseHeight - lv.Top)
    End Sub


    Private Sub lv_ItemClick (Position As Int, Value As Object)
        et.Text = Value
        et.SelectionStart = et.Text.Length
        
    ime.HideKeyboard
        lv.Visible = 
    False
        lblcl.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
        lblcl.Visible=
    True
        lv.Color=
    Colors.RGB(68,68,68)
        
    End If
        
    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)

    End Sub

    Private Sub AddItemsToList(li As List, full As String)
        
    If li.IsInitialized = False Then Return
        
    Dim cs As CSBuilder
        
    For i = 0 To li.Size - 1
            
    Dim item As String = li.Get(i)
            
    Dim x As Int = item.ToLowerCase.IndexOf(full)
            
    If x = -1 Then
                
    Continue
            
    End If
            cs.Initialize.Append(item.SubString2(
    0, x)).Color(highlightColor).Append(item.SubString2(x, x + full.Length)).Pop
            cs.Append(item.SubString(x + full.Length))
            lv.AddSingleLine(cs)
        
    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 ListAs 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

    Sub lblcl_click
        lv.Visible=
    False
        et.Text=
    ""
        lblcl.Visible=
    False

    End Sub
     
    Last edited: Oct 24, 2017
  2. tufanv

    tufanv Expert Licensed User

    et.SelectionStart = et.Text.Length worked to hide the listview but searchviews et is still focused , is there any way to remove the focus ?
     
    Last edited: Oct 24, 2017
  3. Ricky D

    Ricky D Well-Known Member Licensed User

    T
    Try putting an invisible edittext on and send it the focus
     
  4. Erel

    Erel Administrator Staff Member Licensed User

    You can also try to temporary disable the text field:
    Code:
    et.Enable = False
    Sleep(
    50)
    et.Enable = 
    True
     
  5. tufanv

    tufanv Expert Licensed User

    already tried it but thanks anyway :)

    putting this under lblcl_click did not work but if i just add et.enabled=false at the end , It doesnt get the focus , but I have to re enable them :)
     
  6. Erel

    Erel Administrator Staff Member Licensed User

    Why aren't you hiding the listview in TextChanged event when New = "" ?
     
  7. tufanv

    tufanv Expert Licensed User

    I also tried that but putting this does not hide anything when I type stg and backspace everything.
    Code:
    If New="" Then
            lv.Visible=
    False
            lblcl.Visible=
    False
            
    ime.HideKeyboard
        
    End If
    exact code is :

    Code:
    Private Sub et_TextChanged (Old As String, New As String)
       
        lv.Clear
        
    If lv.Visible = False Then
        lv.Visible = 
    True
        lblcl.Visible=
    True
        lv.Color=
    Colors.RGB(68,68,68)
        
    End If
        
    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)
       
        
    If New="" Then
            lv.Visible=
    False
            et.SelectionStart = et.Text.Length
            lblcl.Visible=
    False
            
    ime.HideKeyboard
        
    End If

    End Sub
     
    Ricky D likes this.
  8. Erel

    Erel Administrator Staff Member Licensed User

    I think that it will be easier if you upload the project (or a small project with only the modified search view).
     
  9. Multiverse app

    Multiverse app Active Member Licensed User

    Have the same problem
     
  10. Ricky D

    Ricky D Well-Known Member Licensed User

    W
    I'm sure the only way to take focus away you must focus on something else. Why do you say that doesn't work? If it's because of the keyboard then simply hide it on the gotfocus event of the view getting the focus
     
    Multiverse app likes this.
  11. Multiverse app

    Multiverse app Active Member Licensed User

    Done! Thanks!
     
    Ricky D likes this.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice