11

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.

Chin
  • 19,717
  • 37
  • 107
  • 164
  • you want to select text from webview?? – Chaudhary Amar Dec 07 '15 at 04:08
  • 1
    http://stackoverflow.com/questions/6058843/android-how-to-select-texts-from-webview http://stackoverflow.com/questions/10448717/android-text-selection-in-webview – Chaudhary Amar Dec 07 '15 at 04:12
  • 1
    Refer this article: [webview text selection](http://stackoverflow.com/questions/6058843/android-how-to-select-texts-from-webview) – Yogesh Patel Dec 07 '15 at 04:14
  • @Amarbir Singh see my edit – Chin Dec 07 '15 at 04:21
  • Use this param in your Edittext-> android:selectAllOnFocus="true" http://stackoverflow.com/questions/9128297/how-to-dyamically-select-text-from-edittext-onclicklistener – Chaudhary Amar Dec 07 '15 at 04:27
  • I think the problem is WebView specific. I tried the sample project and I was able so select text on EditText – Msp Jan 01 '16 at 18:16
  • Really? Did the context menu appear when you select the text? Were you able to copy that text? – Chin Jan 01 '16 at 18:19
  • Problem is specific to web view. I have also tried the sample provide and context menu shows up edit text without any problem – Santosh Jan 02 '16 at 07:45
  • If it works when and activity is bound to it then thats the way to go. Android lifecycle is pretty straightforward. It should not be a reason for you to change how you do things just because you think its hard to handle the lifecycle... Plus I have no idea what you are trying to accomplish with this "rogue" overlay view. Its like "I will make this activity layout but its so this random service can load it into view, not the activity itself". You're reinventing a wheel my friend. – Arnold Balliu Jan 02 '16 at 08:00
  • @ArnoldB: dealing with the lifecycle of my app's activity is not the problem, it's the lifecycle of the other running app in the system that is the problem. With a view not attached to any activity, you can show/hide it without disrupting the activity it is overlayed onto (e.g. that activity won't have to be paused when the view is shown, and won't have to be resumed when the view is hidden away). A lot of undesirable things can happen because of the other app pausing/resuming, for example the YouTube app would pause the current playing video. – Chin Jan 03 '16 at 23:47
  • @Santosh what Android version did you try the sample on? – Chin Jan 03 '16 at 23:51
  • Android 4.4.4 , its not a stock Android (Xiomi phone) – Santosh Jan 04 '16 at 07:39

1 Answers1

0

There is a lot of debate over the question whether you can create views from a service. The problem is that you cant really have full functionality and that is probably the problem you are facing right now with text not being selectable or copyable. I'm relatively sure its because callbacks are not registered to a running instance of an activity.

As stated in an answer in this question creating a view from a service is "against Google guidelines"

How to display a Dialog from a Service

Here people are debating over it but in the end THIS IS ACTUALLY AGAINST GOOGLE GUIDELINES. You can try and make it work but your app wont make it to the Play Store.

Starting a View from a Service?

If you read this as well: https://developer.android.com/reference/android/view/View.html

You will find that you need a "tree of views" which is usually attached to an activity to have functionality

Once you have created a tree of views, there are typically a few types of common operations you may wish to perform:

Set properties: for example setting the text of a TextView. The available properties and the methods that set them will vary among the different subclasses of views. Note that properties that are known at build time can be set in the XML layout files.

Set focus: The framework will handled moving focus in response to user input. To force focus to a specific view, call requestFocus().

Set up listeners: Views allow clients to set listeners that will be notified when something interesting happens to the view. For example, all views will let you set a listener to be notified when the view gains or loses focus. You can register such a listener using setOnFocusChangeListener(android.view.View.OnFocusChangeListener). Other view subclasses offer more specialized listeners. For example, a Button exposes a listener to notify clients when the button is clicked.

Set visibility: You can hide or show views using setVisibility(int).

Have a nice day.

Arnold Balliu
  • 1,099
  • 10
  • 21