15

I've tried a lot, but can't make work an AutoCompleteTextView float hint using the TextInputLayout from support.

It's possible or I need to use an external library?

N J
  • 27,217
  • 13
  • 76
  • 96
  • You can definitely do it using Design Library. Post your code so I can help you better. – Danilo Prado Aug 13 '15 at 13:07
  • 2
    Please explain what "can't make work" means. In other words, you need to provide us with a [minimal, complete, and verifiable example](http://stackoverflow.com/help/mcve) of your problem. – CommonsWare Aug 13 '15 at 13:13

2 Answers2

29

No .

design library itself is enough

include like this

<android.support.design.widget.TextInputLayout
    android:id="@+id/til_id"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="125dp"
    >

    <AutoCompleteTextView
        android:id="@+id/auto_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Id"
        android:singleLine="true"/>
</android.support.design.widget.TextInputLayout>
Community
  • 1
  • 1
N J
  • 27,217
  • 13
  • 76
  • 96
  • Thanks, i have an especific layout that doesn't work, i'm trying to figure out what was my mistake. – Marcelo Baccelli Aug 13 '15 at 13:29
  • what are you trying to show on that link? totally useless. Can you actually point it to a specific class or line in the code? – delkant Sep 18 '16 at 19:54
  • @delkant there was code for answer, but over the time repo is updated. Thanks for pointing – N J Sep 19 '16 at 02:12
  • 1
    Thank you for the clarification! – delkant Sep 19 '16 at 02:16
  • 2
    2020, and this still works. though i would like to point out that there would be a small design issue since `TextInputEditText` adds a padding which `AutoCompleteTextView` doesn't .So, Adding a padding `android:padding="16dp"` to `AutoCompleteTextView` will make it look exactly similar as before – ansh sachdeva Mar 27 '20 at 18:57
0

It's maybe a little bit late but here is the trick:

Create the following Class,

public class AutoCompleteTextInputLayout extends TextInputLayout {

    private boolean mIsHintSet;
    private CharSequence mHint;

    public AutoCompleteTextInputLayout(Context context) {
        super(context);
    }

    public AutoCompleteTextInputLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public void addView(View child, int index, ViewGroup.LayoutParams params) {
        if (child instanceof EditText) {
            mHint = ((EditText) child).getHint();
        }
        super.addView(child, index, params);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        if (!mIsHintSet && ViewCompat.isLaidOut(this)) {
            setHint(null);

            EditText editText = getEditText();
            if (editText == null) {
                return;
            }
            CharSequence currentEditTextHint = editText.getHint();
            if (!TextUtils.isEmpty(currentEditTextHint)) {
                mHint = currentEditTextHint;
                editText.setHint("");
            }
            setHint(mHint);
            mIsHintSet = true;
        }
    }
}

Now Add this lines to your layout file and see the magic

<com.example.AutoCompleteTextInputLayout
    android:id="@+id/text_input_autocomplete"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <AutoCompleteTextView
        android:id="@+id/autocomplete_tv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:singleLine="true"
        android:textColorHint="@color/textColorDarkHint"/>
</com.example.AutoCompleteTextInputLayout>
koni
  • 1,805
  • 1
  • 14
  • 13