Android Question AutoComplete EditText

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
Hi to everybody
I have a list of strings and an EditText.
I would like that, as soon as the user writes something on the EdiText, a search is done on the list and, if any element of the List contains partially the entered text, this latter will be replaced by the List element.
The idea is explained in the attached project. Here we have an EditText and a Label. The Label changes, of course. The problem is to change the EditText, (for obvious reasons).
Thanks for any hint.
B4X:
#Region  Project Attributes 
    #ApplicationLabel: B4A Example
    #VersionCode: 1
    #VersionName: 
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes 
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Private xui As XUI
    Dim ListaPunti As List
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    Private EditText1 As EditText
    Private Label1 As Label
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Layout")
    ListaPunti.Initialize
    ListaPunti.Add("1111")
    ListaPunti.Add("2222")
    ListaPunti.Add("3333")
    ListaPunti.Add("4444")
    ListaPunti.Add("5555")
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Private Sub EditText1_TextChanged (Old As String, New As String)
    
    Label1.Text=""
    If ListaPunti.IsInitialized Then
        For Each b As String In ListaPunti
            If b.Contains(New) Then
                Label1.Text=b
                Exit
            End If
        Next
    End If
End Sub
 

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
Hi. I have an additional question: as soon as I digit a part of an existing name, the autocomplete works. Let's suppose to have also a Button that needs the entire name (I just wrote a part of it, and the Button is pressed just after my partial input of the name): ho to get the entire autocompleted text? I attach a modified version of my code, using SuggestionText, to eplain the question.
B4X:
#Region  Project Attributes 
    #ApplicationLabel: B4A Example
    #VersionCode: 1
    #VersionName: 
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes 
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Private xui As XUI
    Dim ListaPunti As List
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    Private EditText1 As SuggestionsEditText
    Private Label1 As Label
End Sub

Sub Activity_Create(FirstTime As Boolean)
    
    Activity.LoadLayout("Layout")
    'EditText1.GetForegroundEditText.Hint = "City"
    ListaPunti.Initialize
    ListaPunti.Add("1111")
    ListaPunti.Add("1222")
    ListaPunti.Add("1333")
    ListaPunti.Add("1244")
    ListaPunti.Add("2555")
    EditText1.DeleteAllWordsDBs("")
    EditText1.InitWordsDB
    EditText1.AddAll(ListaPunti)
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Private Sub Button1_Click
    Label1.Text=EditText1.Text ' ???    
End Sub
 

Attachments

  • TextEditText.zip
    9.5 KB · Views: 7
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
I attach a modified version of my code, using SuggestionText, to eplain the question.
I get what you mean, similar to B4X's autocomplete, where you use the TAB key, but you actually want to use a Button.

I guess I should modify that library, though.
I'm going to try to see if I can do it without modifications.

But I don't understand the purpose; it would only be useful if you wanted to disable automatic search.
 
Upvote 0

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
I get what you mean, similar to B4X's autocomplete, where you use the TAB key, but you actually want to use a Button.

I guess I should modify that library, though.
I'm going to try to see if I can do it without modifications.

But I don't understand the purpose; it would only be useful if you wanted to disable automatic search.
I have some names in a list. The user writes a part of the name. The entire name appears (without the user writes it completely). So, when the user sees the correct name, he can press the button to accept it. That's all. Thanks
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
I'm back

I saw that the text is completed/confirmed when you press "Enter" on the keyboard, which does what you want a button to do.

If you want, I can change it anyway, but is it worth it? Isn't it better to have one less button?

[It only takes me a second to make the change; I just need to change the scope of a Sub from Private to Public. The "longest" time would be compiling and publishing, that's all].
 
Last edited:
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
I'm back

I saw that the text is completed/confirmed when you press "Enter" on the keyboard, which does what you want a button to do.

If you want, I can change it anyway, but is it worth it? Isn't it better to have one less button?

[It only takes me a second to make the change; I just need to change the scope of a Sub from Private to Public. The "longest" time would be compiling and publishing, that's all].
Added method ConfirmWord (library version 2.2)
 

Attachments

  • TextEditText.zip
    9.6 KB · Views: 11
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
Hai guardato il tuo PayPal? 🤣
Si, mannaggia, stavo per ringraziarti pubblicamente e, cosa rara, sono stato interrotto da una telefonata!
Yes, I was about to thank you publicly and, rare thing, I was interrupted by a phone call!

Ho bisogno di un caffè (reale
REALE - andato in alla!!! cucina, versato dalla MOKA e scaldato al microonde! NON ti stavo chiedendo una donazione, maremma decaffeinata 😄
REAL - went to the kitchen, poured from the MOKA pot, and heated in the microwave! I was NOT asking for a donation, "maremma decaffeinata"! 😄

Ti ringrazio, vorrà dire che con la donazione comprerò pacchetti di caffè per un mese (credo, boh).
Thank you, it will mean that with the donation I will buy packets of coffee for a month (maybe, who knows).

You didn't attach the modified library, If I don't miss something
La trovi nello stesso thread di cui ti ho dato il link:
You can find it in the same thread I linked to:
https://www.b4x.com/android/forum/threads/custom-view-suggestionsedittext.77776/#content
 
Upvote 0

GoldenAura

New Member
You can do this by adding a TextWatcher to your EditText. In the onTextChanged method, check your list for matches, and if one contains the typed text, set the EditText text to that item. Just make sure to handle the cursor position so it doesn’t jump around.
 
Upvote 0

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
Only another suggestion, due to my tests. When we use ConfirmWord, the cursor goes to the beginning of the EditText. I think (but not totally sure) that if the cursor goes to the end could be better.
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
Only another suggestion, due to my tests. When we use ConfirmWord, the cursor goes to the beginning of the EditText. I think (but not totally sure) that if the cursor goes to the end could be better.
Since the user has accepted the "suggestion", he will most likely keep it as is and move on to another input field, OR he may have changed his mind or wanted to choose a word/phrase, so perhaps it would be better to have all the text automatically selected and the cursor at the beginning, so that if he starts typing, whatever is already in the EditText will be deleted and the automatic search will be started.

(Certo che in italiano è "leggermente" più facile 😄)
 
Upvote 0
Top