19

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?

Octavian Helm
  • 39,405
  • 19
  • 98
  • 102
Tonio34
  • 191
  • 1
  • 4

1 Answers1

5

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.

Community
  • 1
  • 1
MH.
  • 45,303
  • 10
  • 103
  • 116
  • Please note: The view you specify must be only a TextView, you cannot wrap it into a Layout, as it will crash with `java.lang.IllegalStateException: The specified child already has a parent.` message. Seems that the OS calls `findViewById`, and attaches it to the DropDown (sounds like a bug to me). This happened to me in SDK 28. Didn't check any other SDK. – lionscribe Jul 03 '19 at 19:47