Android ListView OnItemClickListener not working with unfocused PopupWindow
In my android app I have EditText
. When focus EditText
is displayed PopupWindow
, which contains ListView
. The user can continue to enter text EditText
when the screen is displayed PopupWindow
and at the same time he / she should be able to click on the item ListView
to reject PopupWindow
. (Depending on which element is ListView
clicked, PopupWindow
something else should happen besides the rejection , but that doesn't matter here.)
The problem is that while the "keep typing" part works, the "click" part doesn't work because it OnItemClickListener
ListView
never gets called and I can't figure out why.
Here's mine activity_main.xml
:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ddd"
android:focusableInTouchMode="true"
tools:context="com.example.popuptest3.MainActivity" >
<EditText
android:id="@+id/edtPhone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="phone" />
</RelativeLayout>
My popup_window.xml
:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fff" >
<ListView
android:id="@+id/lstItems"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="60dp" />
<Button
android:id="@+id/btnDismiss"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="Dismiss" />
</RelativeLayout>
My list_item.xml
:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="50dp" >
<TextView
android:id="@+id/txtItem"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:text="Item Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
In my class MainActivity
, I have the following preparation code:
private PopupWindow popup = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
prepareControls();
}
private void prepareControls() {
final EditText edtPhone = (EditText)findViewById(R.id.edtPhone);
edtPhone.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus)
openPopup(edtPhone);
else
closePopup();
}
});
}
That is, I open PopupWindow
when it edtPhone
gains focus, and close PopupWindow
when it edtPhone
loses focus. Then I have:
private void openPopup(View parent) {
closePopup();
// Inflate the view of the PopupWindow.
LayoutInflater layoutInflater = LayoutInflater.from(this);
View view = layoutInflater.inflate(R.layout.popup_window,
new LinearLayout(this), false);
final Activity activity = this;
// Prepare the ListView.
ListView lstItems = (ListView)view.findViewById(R.id.lstItems);
lstItems.setAdapter(new ListAdapter(this));
lstItems.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
hideKeyboardAndClearFocus(activity);
}
});
// Prepare the Button.
Button btnDismiss = (Button)view.findViewById(R.id.btnDismiss);
btnDismiss.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
hideKeyboardAndClearFocus(activity);
}
});
// Create and show the PopupWindow.
popup = new PopupWindow(view, 400, 300, false);
popup.showAtLocation(parent, Gravity.LEFT | Gravity.TOP, 0, 60);
}
The button is btnDismiss
not part of my plan; I am adding it here for demonstration purposes only. Finally, I:
private void closePopup() {
if (popup != null) {
popup.dismiss();
popup = null;
}
}
private static void hideKeyboardAndClearFocus(Activity activity) {
InputMethodManager manager = (InputMethodManager)activity.
getSystemService(Activity.INPUT_METHOD_SERVICE);
manager.hideSoftInputFromWindow(
activity.getCurrentFocus().getWindowToken(), 0);
activity.getWindow().getDecorView().clearFocus();
}
I think this is self-explanatory and the class ListAdapter
is pretty standard, so I omit it.
Now when I click edtPhone
it gets focus, the soft keyboard comes up and opens PopupWindow
. I can type something in edtPhone
, and when I click on btnDismiss
, it edtPhone
loses focus, the soft keyboard rejects and PopupWindow
closes as expected. But if instead btnDismiss
I click the element on lstItems
, nothing happens. Using Log
I can see what is OnItemClickListener
not being called. The funny thing is that ListView
you can scroll well, it just can't be clicked.
I know this post , but this solution doesn't work for my purpose. If I change this line:
popup = new PopupWindow(view, 400, 300, false);
to the next:
popup = new PopupWindow(view, 400, 300, true);
PopupWindow
becomes customizable and ListView
becomes clickable, okay, but then I can't keep typing in edtPhone
. In fact, if there are other activity controls, they all become inaccessible, as if it were PopupWindow
"modal".
I also tried android:focusable="false"
with no luck. And I am not doing autocomplete, so AutoCompleteTextView
not what I need.
So why is OnItemClickListener
n't it called for non-focusable ones PopupWindow
? Am I doing something wrong?
source to share
set popupwindow to non-focusable
popupwindow.setFocusable(false);
popupwindow.setTouchable(true);
popupwindow.setOutsideTouchable(true);
extend ListView and override following methods
@Override
public boolean hasFocus() {
return true;
}
@Override
public boolean isFocused() {
return true;
}
@Override
public boolean hasWindowFocus() {
return true;
}
So AutoCompleteTextView implements popup for suggestions. If you need to select a list item using DPAD keys or other navigation keys, you need to pass key events from the EditText to the ListView.
source to share
I solved this problem by simply setting OnClickListener
to convertview
which is the parameter of the adapter method getView
. Then I call the onItemClick
listview onItemClickListener method .
No need to customize popupWindow . Infact, just using ListPopupWindow is easier as its only internally responsive method getView()
is that we add a few lines of code
ListPopupWindow popupWindow = new ListPopupWindow(mContext);
MentionsAdapter adapter = new MentionsAdapter(mContext, suggestions);
popupWindow.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();
}
});
popupWindow.setAdapter(adapter);
MentionsAdapter
getView
method
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null){
convertView = LayoutInflater.from(context).inflate(,R.layout.item_user,parent,false);
}
// this is the key point of workaround
convertView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
popupWindow.getListView().getOnItemClickListener()
.onItemClick(listView, v, position, getItemId(position));
}
});
return convertView;
}
source to share