Android Question Search key on keyboard?

ArminKH

Well-Known Member
hi
how is possible to use search button in keyboard?
i have an edit text and i want to search for entered text in my txt file
but i want to use keyboard search button
is this possible?
upload_2015-8-15_20-45-2.png
 

stevel05

Expert
Licensed User
Longtime User
Have you tried using an Activity keypress sub?

B4X:
Sub Activity_KeyPress (KeyCode As Int) As Boolean 'Return True to consume the event
    If KeyCode = KeyCodes.KEYCODE_SEARCH Then
        'Search pressed
        'your search code goes here
        Return True
    End If
End Sub
 
Upvote 0

ArminKH

Well-Known Member
Have you tried using an Activity keypress sub?

B4X:
Sub Activity_KeyPress (KeyCode As Int) As Boolean 'Return True to consume the event
    If KeyCode = KeyCodes.KEYCODE_SEARCH Then
        'Search pressed
        'your search code goes here
        Return True
    End If
End Sub
i can handle the search key event but how can i show search button(icon) for my edit text?
please look at the above screen shot
 
Upvote 0

ArminKH

Well-Known Member
@stevel05
my problem solved by this hint
http://stackoverflow.com/questions/23528644/edittext-ignores-the-imeactionlabel
but as you can see in above link we should create a text box with xml
but i want to use this for text boxes which are created by designer and also i want to use this for AppCombat Edittext
is there any way to set android:imeOptions="actionSearch" by using java object to an edittext?
this code not works
B4X:
Dim r As Reflector
         r.Target = EditText1
         r.RunMethod2("setImeOptions", 4, "java.lang.int")
 
Last edited:
Upvote 0

stevel05

Expert
Licensed User
Longtime User
I was just searching for the answer.

To display a search icon instead of the return key, you can use:

B4X:
Dim JO As JavaObject = EditText1
    JO.RunMethod("setImeOptions",Array As Object(3))

The key will then be treated as if you pressed the return key.

In addition, you can set the onEditorActionListener to respond to the key.

B4X:
Dim O As Object = JO.CreateEvent("android.widget.TextView.OnEditorActionListener","Search",Null)
    JO.RunMethod("setOnEditorActionListener",Array As Object(O))

Which will call the sub:

B4X:
Sub Search_Event(MethodName As String,Args() As Object) As Object
    Log(MethodName & " called")
    Return True
End Sub

When it is pressed, return False instead of true to close the keyboard.
 
Upvote 0

ArminKH

Well-Known Member
I was just searching for the answer.

To display a search icon instead of the return key, you can use:

B4X:
Dim JO As JavaObject = EditText1
    JO.RunMethod("setImeOptions",Array As Object(3))

The key will then be treated as if you pressed the return key.

In addition, you can set the onEditorActionListener to respond to the key.

B4X:
Dim O As Object = JO.CreateEvent("android.widget.TextView.OnEditorActionListener","Search",Null)
    JO.RunMethod("setOnEditorActionListener",Array As Object(O))

Which will call the sub:

B4X:
Sub Search_Event(MethodName As String,Args() As Object) As Object
    Log(MethodName & " called")
    Return True
End Sub

When it is pressed, return False instead of true to close the keyboard.

thank u steve but this is still not works on my device
B4X:
Dim EditText1 As EditText
            EditText1.Initialize("EditText1")
            Activity.AddView(EditText1 , 0,0,100dip,50dip)
            Dim JO As JavaObject = EditText1
            JO.RunMethod("setImeOptions",Array As Object(3))
 
Upvote 0

ArminKH

Well-Known Member
@stevel05
Solved
the Edit Text must be single line
now this code works
B4X:
Dim EditText1 As EditText
            EditText1.Initialize("EditText1")
            Activity.AddView(EditText1 , 0,0,100dip,50dip)
            EditText1.SingleLine = True
           
            Dim JO As JavaObject = EditText1
                JO.RunMethod("setImeOptions",Array As Object(3))
thanx for your help;)
 
Upvote 0
Top