Android OnClick event listener change in API 23/24 -
i see mismatch in behavior between api 19 , api 23/24 regarding click/touch events. below sample code has button create popup window, , button in new window close it. testing on device api 19 works expected, if click on popup window's button, window closes, if click on anywhere else, nothing happens. running same code api 23 or 24, if click next popup window, disappears, without executing event handler.
can me out why happening, , how make sure behavior consistent see on api 19? or alternatively, suggest ways troubleshoot problem?
mainactivity.java
public class mainactivity extends appcompatactivity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); } public void openpopup(view v) { layoutinflater inflater = (layoutinflater) getsystemservice(context.layout_inflater_service); view layout = inflater.inflate(r.layout.popup, null, false); final popupwindow pw = new popupwindow(layout); pw.setwidth(linearlayout.layoutparams.wrap_content); pw.setheight(linearlayout.layoutparams.wrap_content); pw.setfocusable(true); pw.showatlocation(findviewbyid(r.id.activity_main), gravity.center, 0, 0); button b = (button) layout.findviewbyid(r.id.popup_button); b.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { pw.dismiss(); } }); } }
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <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:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" tools:context="com.danielsh.games.test.mainactivity" android:id="@+id/activity_main"> <android.support.v7.widget.appcompatbutton android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="open popup" android:onclick="openpopup"/> </relativelayout>
popup.xml
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/coloraccent"> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="some text" android:textcolor="@color/colorprimarydark" android:textsize="24sp"/> <button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="close" android:id="@+id/popup_button"/> </linearlayout>
try using if want common handling when dismissing window: https://developer.android.com/reference/android/widget/popupwindow.ondismisslistener.html
pw.setondismisslistener(new popupwindow.ondismisslistener() { // place handler code here });
and if not want window closed if user touches outside of popup (note: in popupwindow these in default):
pw.settouchable(true); pw.setfocusable(false); pw.setoutsidetouchable(false);
reference this: https://developer.android.com/reference/android/widget/popupwindow.html#setoutsidetouchable(boolean)
this makes sense pop-ups touchable not focusable, means touches outside of window delivered window behind.
edit: ok, reproduce problem , can confirm if remove line pw.setfocusable(true);
code work wanted.
Comments
Post a Comment