Pardeep Kumar
Pardeep Kumar

Reputation: 950

How to set autocomplete text inside EditText in android

I have an EditText. when i type something, i do a DB query based on the typed string and show the results in a recycler view. i want to show the first result item from the query result in the edit text itself in grey colour. For eg.

I type "sa"

Results are shown as :

"saaa" "sab" "sacc"

i want that once the result are fetched, Edit text should show first item: "sa|aa"

here "sa" will be in black colour, then cursor , then aa will be in grey colour.

so the user gets the autocomplete kind of feel.

The problem i am facing is , how i set the text on edit text with two different strings both of different colours. I tried setting both text and hint, looks like hint does not work when text is set. Whats the proper way to do it ?

Upvotes: 0

Views: 202

Answers (1)

Mohit Ajwani
Mohit Ajwani

Reputation: 1338

You can check this StackOverflow answer for general understanding. Check the developer website for in-depth knowledge about Spans

SpannableStringBuilder spannable = new SpannableStringBuilder("Text is spantastic!");
spannable.setSpan(
    new ForegroundColorSpan(Color.RED),
    8, // start
    12, // end
    Spannable.SPAN_EXCLUSIVE_INCLUSIVE
);

Sample Output for Spannable

Upvotes: 1

Related Questions