I have an autocompletetextview which I have linked it to a webservice so it shows me suggestions as I type. Now how can I hide the soft keyboard when the user starts scrolling through the autocomplete dropdown? I looked through the net but didnt find any method to detech touches on the autocomplete dropdown.
The best solution I could come up for this, is hiding the keyboard when the user starts scrolling the list and showing the keyboard again, if the user touches on the textview again. This pretty much works for most of the OS versions and devices, unlike other solutions you could see, such as setting the height of the dropDownHeight.
Below is a sample code to hide the keyboard, when the user starts scrolling. Basically, you need to create a touch listener in your AutoCompleteTextView's adapter.
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ViewHolder holder;
if (convertView == null) {
convertView = inflater.inflate(viewResourceId, parent, false);
holder = new ViewHolder();
init(convertView, holder);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
convertView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
InputMethodManager imm = (InputMethodManager) getContext()
.getSystemService(
Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(
searchView.getWindowToken(), 0);
}
return false;
}
});
setView(position, holder);
return convertView;
}
I would take this answer, or @ayorhan's answer as accepted answer, it really is the best way to handle dismissing the keyboard when scrolling a dropdown selection.
This is a play off @ayorhan's solution, for use with a SimpleCursorAdapter. I had to make a custom SimpleCursorAdapter class:
public class SimpCursAdap extends SimpleCursorAdapter {
public SimpCursAdap(Context context, int layout, Cursor c, String[] from, int[] to, int flags) {
super(context, layout, c, from, to, flags);
}
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
view.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
InputMethodManager imm = (InputMethodManager) getContext()
.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(
view.getApplicationWindowToken(), 0);
}
return false;
}
});
return view;
}
}
Then you can instantiate the class anywhere:
final SimpleCursorAdapter adapter = new SimpCursAdap(aContext,
aRowLayout,
null,
aColNames,
aRowViewsIds,
0);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
adapter.setStringConversionColumn(aValueColId);
autocompletetextview.setAdapter(adapter);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With