I've spent the best amount of the last two days puzzling on this. Actually I've wanted a spinner which behaves like a EditText inside a TextInputLayout (fancy hint, which flows away and is selected/entered if in the previous edittext the next/enter keyboard button is pressed).
This seemed to be pretty impossible, so i came up with this:
<android.support.design.widget.TextInputLayout
android:id="@+id/newMeasure_layout"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="@+id/title_layout"
app:layout_constraintStart_toStartOf="@+id/title_layout"
app:layout_constraintTop_toBottomOf="@+id/measureSpinner">
<AutoCompleteTextView
android:id="@+id/newMeasure"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:imeOptions="actionNext|flagNoExtractUi"
android:singleLine="true"
android:inputType="textNoSuggestions|textVisiblePassword"
android:cursorVisible="false"
android:hint="@string/measure"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:drawableTint="@color/secondaryColor"
android:drawableEnd="@drawable/ic_arrow_drop_down_black_24dp"
android:drawableRight="@drawable/ic_arrow_drop_down_black_24dp" />
</android.support.design.widget.TextInputLayout>
this prevents the keyboard from showing a blinking cursor, providing suggestions, corrections, ...
To prevent the user typing something, but still allow focusing the next input by pressing enter / next, i've set up a filter in the code (which also checks if the text is available in the suggestions-cursor).
private AutoCompleteTextView mNewMeasure;
...
mNewMeasure = root.findViewById(R.id.newMeasure);
mNewMeasure.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
((AutoCompleteTextView)view).showDropDown();
return false;
}
});
mNewMeasure.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean b) {
AutoCompleteTextView v = ((AutoCompleteTextView)view);
if(b && v.getText().length() == 0) {
v.showDropDown();
}
}
});
//inside the cursor loaded method (data == the loaded cursor)
String[] madapterCols = new String[]{1}; //0 contains the id, 1 the textfield
int[] madapterRowViews = new int[]{android.R.id.text1};
SimpleCursorAdapter msca = new SimpleCursorAdapter(
getContext(),
R.layout.support_simple_spinner_dropdown_item,
data,
madapterCols,
madapterRowViews,
SimpleCursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
msca.setDropDownViewResource(R.layout.support_simple_spinner_dropdown_item);
msca.setStringConversionColumn(1);
mNewMeasure.setAdapter(msca);
//NOTE: its onItemClick for the suggestions, instead of onItemSelected as the spinner requires
// https://stackoverflow.com/questions/9616812/how-to-add-listener-to-autocompletetextview-android
mNewMeasure.setOnItemClickListener(new AdapterView.OnItemClickListener() {...});
InputFilter infil = new InputFilter() {
//based on https://stackoverflow.com/questions/37152413/allowing-space-and-enter-key-in-android-keyboard
@Override
public CharSequence filter(CharSequence charSequence, int i, int i1, Spanned spanned, int i2, int i3) {
Pattern ps = Pattern.compile("^[\n]+$");
if(!charSequence.equals("")
&& !cursorContains(data, charSequence)
&& !ps.matcher(charSequence).matches()) {
//not allowed
return "";
}
return null;
}
private boolean cursorContains(Cursor c, CharSequence cs) {
c.moveToFirst();
int textColIdx = 1; // c.getColumnIndex(PoetcomContract.)
for(int i = 0; i < c.getCount(); i++) {
String currentCursorStringval = c.getString(textColIdx);
if(currentCursorStringval.equalsIgnoreCase(cs.toString())) {
return true;
}
c.moveToNext();
}
return false;
}
};
mNewMeasure.setFilters(new InputFilter[] {infil});
And the result:
A different approach - with similar intent and outcome, which uses the EditText as Filter for the attached adapter: https://stackoverflow.com/a/43971008