Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AutocompleteTextview and CompletionHintView

I'm currently implementing an autocomplete field by using the AutocompleteTextView component.

I'm trying to add a completion hint with the number of results, and just want to style it differently from the dropdown list element. There is an attribute named completionHintView on the component, but every time I give it a layout I previously defined it throws a NullPointerException.

Does anyone have a working example on how to style the completion hint?

like image 779
Tonio34 Avatar asked Aug 31 '10 20:08

Tonio34


People also ask

What is AutoCompleteTextView?

An editable text view that shows completion suggestions automatically while the user is typing. The list of suggestions is displayed in a drop down menu from which the user can choose an item to replace the content of the edit box with.

What is the purpose of AutoCompleteTextView control?

Android AutoCompleteTextView is an editable text view which shows a list of suggestions when user starts typing text. When a user starts typing, a dropdown menu will be there based on the entered characters, defined in the threshold limit and the user can choose an item from the list to replace the text.

Which method is used for filling the data to AutoCompleteTextView?

Android AutoCompleteTextView Overview Some important methods of Autocomplete list are given below: getAdapter() : This method returns a filterable list adapter used for auto completion.


1 Answers

Can you elaborate on what kind of styling you are seeking to apply?

If it's just basic text styling, you could probably build a Spannable and set the completion hint with the result, since it accepts a CharSequence. An example of building a Spannable and apply styles to it is illustrated in this post.

If you're looking for a way to actually manipulate the parameters of the TextView (e.g. padding), the source code of AutoCompleteTextView seems to provide a hint (pun intended).

private View getHintView(Context context) {
    if (mHintText != null && mHintText.length() > 0) {
        final TextView hintView = (TextView) LayoutInflater.from(context).inflate(
                mHintResource, null).findViewById(com.android.internal.R.id.text1);
        hintView.setText(mHintText);
        hintView.setId(HINT_VIEW_ID);
        return hintView;
    } else {
        return null;
    }
}

This reveals that Android looks for the id text1 in the resource reference you specify. The most basic version of such a resource would contain nothing but a TextView with this id:

<?xml version="1.0" encoding="utf-8"?>
    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@android:id/text1" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:padding="10dp"
        android:textColor="#FF0000" android:textSize="16sp" />

Save above in a layout file (e.g. completion_hint_view.xml) and reference it as follows from your AutoCompleteTextView:

<AutoCompleteTextView android:id="@+id/autocomplete_textview"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:completionHintView="@layout/completion_hint_view"/>

This second option is probably the easiest to use and will give you full access to the TextView's parameters. If you need to apply multiple styles to the text in this view, you can incorporate the first suggestion, as that will give you more flexibility.

If neither of these suggestions suffice, I can think of some less elegant work arounds that would probably allow you to get the same result.

like image 147
MH. Avatar answered Sep 21 '22 16:09

MH.