Ok, I have the following requirements:
- An EditText in which if the user types @ a popup with suggestions appears
- The user can continue typing which will filter the suggestions
- The user can tap on a suggestion which will complete the entry and dismiss the popup
- The user can tap outside the popup which will dismiss it
Here is my code:
PopupWindow popupWindow = new PopupWindow(mContext);
// Create ListView to show the suggestions
ListView listView = new ListView(mContext);
listView.setBackgroundColor(Color.WHITE);
MentionsAdapter adapter = new MentionsAdapter(mContext, suggestions);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String username = mSuggestions.get(position);
String mentionCompletion = username.substring(mCurrentQuery.length()).concat(" ");
mEditor.getText().insert(mEditor.getSelectionEnd(), mentionCompletion);
hideSuggestions();
}
});
mSuggestionsListView = listView;
popupWindow.setContentView(listView);
popupWindow.setFocusable(false);
popupWindow.setTouchable(true);
popupWindow.setOutsideTouchable(true);
popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
@Override
public void onDismiss() {
clearPopupData();
}
});
Rect popupRect = calculatePopupPosition();
popupWindow.setWidth(popupRect.width());
popupWindow.setHeight(popupRect.height());
popupWindow.showAtLocation(mEditor, Gravity.START | Gravity.TOP, popupRect.left, popupRect.top);
mPopupWindow = popupWindow;
It does work! The problem is that it works only on some Android versions/devices. For example it works on Android 6.0 on the emulator but it doesn't work on my colleague's LG G4 with 6.0. It doesn't work on Android 4.3.1 but works on 4.4.2.
If the PopupWindow
is not focusable the ListView
's OnItemClickListener
is not called. If the PopupWindow
is focusable the ListView
's OnItemClickListener
is called but EditText
doesn't receive keyboard events.
I tried countless combinations of changing focus/touch modes on the PopupWindow
and ListView
and couldn't get it working in those cases.
Any suggestions on WFT is going on and how to fix it?