TextInputLayout and AutoCompleteTextView
Solution 1:
Now, with AndroidX you don't need customise something.
Need just add material component style (was added in 1.1.0-alpha06
, see release notes).
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Example TextInputLayout">
<androidx.appcompat.widget.AppCompatAutoCompleteTextView
style="@style/Widget.MaterialComponents.AutoCompleteTextView.FilledBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</com.google.android.material.textfield.TextInputLayout>
Solution 2:
A little late, but yes, you'll have to roll your own implementation. The good news is that this is fairly straightforward. Here's how TextInputEditText
was implemented:
https://android.googlesource.com/platform/frameworks/support.git/+/master/design/src/android/support/design/widget/TextInputEditText.java
Accordingly, here's what TextInputAutoCompleteTextView
might look like.
public class TextInputAutoCompleteTextView extends AppCompatAutoCompleteTextView {
public TextInputAutoCompleteTextView(Context context) {
super(context);
}
public TextInputAutoCompleteTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public TextInputAutoCompleteTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
final InputConnection ic = super.onCreateInputConnection(outAttrs);
if (ic != null && outAttrs.hintText == null) {
// If we don't have a hint and our parent is a TextInputLayout, use it's hint for the
// EditorInfo. This allows us to display a hint in 'extract mode'.
final ViewParent parent = getParent();
if (parent instanceof TextInputLayout) {
outAttrs.hintText = ((TextInputLayout) parent).getHint();
}
}
return ic;
}
}
Solution 3:
Building off of chessdork's answer, I thought I'd go more into detail on how you can incorporate an autofill with a hint into your project. Here are the exact steps I used to get it working:
1) Make sure you have implementation 'com.android.support:design:26.1.0'
in your gradle dependencies. The exact package name be slightly different depending on your SDK version.
2) Copy the TextInputAutoCompleteTextView
class from @chessdork's answer and put it in a public class within your project.
3) Place where you want the autofill edittext to be in your XML layout. It should be structured like this:
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="16dp">
<mycompany.views.TextInputAutoCompleteTextView
android:id="@+id/myAutoFill"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/myHint"/>
</android.support.design.widget.TextInputLayout>
Solution 4:
With the Material Components library just use a TextInputLayout
with a Widget.MaterialComponents.TextInputLayout.*.ExposedDropdownMenu
style.
Something like:
<com.google.android.material.textfield.TextInputLayout
style="@style/Widget.MaterialComponents.TextInputLayout.FilledBox.ExposedDropdownMenu"
android:hint="Hint..."
...>
<AutoCompleteTextView
android:background="@null"
.../>
</com.google.android.material.textfield.TextInputLayout>