I'm using TextInputLayout
in my Android app to achieve that neat floating label effect for my input fields. I know that I should be using the TextInputEditText
as well to allow hints to be displayed when in landscape mode and the input are fills the entire screen.
However, in some of my input fields I have got autocompletion going on using AutoCompleteTextView
(which IMO has a really inconsistent name to it – “TextView” instead of “EditText” – but that's another story) and that obviously inherits directly from EditText
. Thus it doesn't have the same functionality as TextInputEditText
brings.
So I'm wondering if there's a way to achieve the same hint-in-landscape functionality (without making my own TextInputAutoCompleteTextView
implementation, that is) and to also avoid the lint warnings that are produced. Am I missing something here? I suppose I get that they didn't make custom versions of all direct and indirect subclasses of EditText
for this specific thing, so am I expected to make my own?
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:
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;
}
}