I am creating a "floating" WebView like this:
public class MyService extends Service {
public static WindowManager windowManager;
public static LinearLayout mainView;
public WebView webView;
@Override
public void onCreate() {
super.onCreate();
windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
// main view
mainView = (LinearLayout) inflater.inflate(R.layout.popup, null);
WindowManager.LayoutParams mainViewParams = new WindowManager.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
PixelFormat.TRANSLUCENT);
windowManager.addView(mainView, mainViewParams);
// main view - web views
webView = (WebView) mainView.findViewById(R.id.webView1);
webView.setWebViewClient(new WebViewClient());
}
}
The main view is a LinearLayout that contains a WebView. The main view is displayed when the service is created, and is not attached to any activity. The view can float on top of other activities thanks to the WindowManager.LayoutParams.TYPE_PHONE
flag.
The problem is, I cannot select any text displayed in the WebView. If I hold my touch on a word, the selection appears then disappears very quickly. The action bar for copying text, etc. doesn't show up, nor is the context menu in Marshmallow.
Is this happening because my view is not attached to an activity? I used to implement my floating view using an activity and text selection worked fine, but I recently changed to this "no activity" approach since I don't want to have to deal with all the unnecessary activity lifecycle management when I show/hide the view.
How can I get back text selection (in my WebView)?
EDIT: this problem is not WebView specific. I have added an EditText into the main view, and I cannot select text from it either.
EDIT 2: Here is an example project showing the problem: https://github.com/chinhodado/floating_test. When you run it, you'll see that it's impossible to select and copy text from the EditText and WebView.