naoak / WebViewMarker

Support library for text selection on Android WebView. (forked from BTAndroidWebViewSelection)
MIT License
62 stars 24 forks source link

Double Selection in Android Lollipop #10

Open DineshNandhagopal opened 9 years ago

DineshNandhagopal commented 9 years ago

Double selection is still occurring in Android 5.0 +. .Eventhough selection is same, as web view marker, CAB is appearing again.Thank you.

naoak commented 9 years ago

Thanks for reporting. I've been away from actual Android development for almost 2 years, so it takes some time for building my android development. I'll check it in 2, 3 days.

DineshNandhagopal commented 9 years ago

Thank you!

naoak commented 9 years ago

I couldn't reproduce it on demo with Nexus 5 emulator (API 21). Do you have any sample code to reproduce it? And what is your specific environment?

DineshNandhagopal commented 9 years ago

Thanks for taking time on it. API build tool is set to Version 23 rc3 and target is API22.

// grade build compileSdkVersion 22 buildToolsVersion "23.0.0 rc3"

defaultConfig {
    applicationId "com.example.webviewmarker"
    minSdkVersion 15
    targetSdkVersion 22
    versionCode 1
    versionName "1.0"
}

This is my CustomView and the codes are below.

public class PageView extends WebView { private String data; private String baseUrl; private SelectActionModeCallback actionModeCallback; private TextSelectionSupport mTextSelectionSupport; private Context context;

public PageView(Context context, AttributeSet attrs) {
     super(context, attrs);
     this.context = context;
     setProperties();
}

public PageView(Context context, String data,String baseUrl){
    super(context);
    this.context = context;
    this.data = data;
    this.baseUrl = baseUrl;
    setProperties();
    this.loadDataWithBaseURL(baseUrl, data, "text/html", "utf-8", null);
}

private void setProperties(){
    getSettings().setJavaScriptEnabled(true);
    getSettings().setAllowFileAccess(true);
    getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
    getSettings().getJavaScriptCanOpenWindowsAutomatically();
    getSettings().setBuiltInZoomControls(true);
    getSettings().setDisplayZoomControls(false);
    setSelectionListener();
    setWebViewClient();
    setWebChromrClient();
}

public void loadPage(String data,String baseUrl){
    this.loadDataWithBaseURL(baseUrl, data, "text/html", "utf-8", null);
}

private void  setWebViewClient() {

    setWebViewClient(new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }

        @Override
        public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
            handler.proceed();
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            view.setVisibility(View.VISIBLE);
        }

        public void onScaleChanged(WebView view, float oldScale, float newScale) {
            mTextSelectionSupport.onScaleChanged(oldScale, newScale);
        }
    });
}
private  void setWebChromrClient() {
    setWebChromeClient(new WebChromeClient() {
        @Override
        public boolean onJsAlert(WebView view, String url, String message, final android.webkit.JsResult result) {
            if (message.startsWith("file:")) {

            } else {
                new AlertDialog.Builder(context)
                        .setTitle("javaScript dialog")
                        .setMessage(message)
                        .setPositiveButton(android.R.string.ok,
                                new AlertDialog.OnClickListener() {
                                    public void onClick(DialogInterface dialog, int which) {
                                        result.confirm();
                                    }
                                })
                        .setCancelable(false)
                        .create()
                        .show();
            }
            return true;
        }

        ;

    });
}

private void setSelectionListener()
{
    mTextSelectionSupport = TextSelectionSupport.support((Activity)context, this);
    mTextSelectionSupport.setSelectionListener(new TextSelectionSupport.SelectionListener() {
        @Override
        public void startSelection() {
        }

        @Override
        public void selectionChanged(String text) {
            Toast.makeText(context,text,Toast.LENGTH_LONG).show();
        }

        @Override
        public void endSelection() {
        }
    });

}

}

// To hide CAB , when double selection,

@Override
public void onActionModeStarted(ActionMode mode) {
    if (mActionMode == null) {
        mActionMode = mode;
        Menu menu = mode.getMenu();
        menu.clear();
        menu.removeItem(android.R.id.home);

        }

   super.onActionModeStarted(mode);
}

public void onContextualMenuItemClicked(MenuItem item) {
    switch (item.getItemId()) {
          default:
            // ...
            break;
    }

    // This will likely always be true, but check it anyway, just in case
    if (mActionMode != null) {
        mActionMode.finish();
    }
}

@Override
public void onActionModeFinished(ActionMode mode) {
    mActionMode = null;
    super.onActionModeFinished(mode);
}

This code is in the activity

naoak commented 9 years ago

Thanks for the details. I'll check it after returning home.

Davory commented 8 years ago

Are there any news on how to handle double selection in Android 5.0 +?

naoak commented 8 years ago

Oh, it completely slipped my mind... I'm sorry that I have no news.

bertrandserres commented 8 years ago

I'm having the same problem. Have you found a solution ? Thanks

naoak commented 8 years ago

Sorry, I have no news..

@DineshNandhagopal, @Davory Have you found any solutions?

defaultbr commented 6 years ago

for future interested -> https://stackoverflow.com/a/28550635