naoak / WebViewMarker

Support library for text selection on Android WebView. (forked from BTAndroidWebViewSelection)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Double Selection in Android Lollipop

DineshNandhagopal opened this issue · comments

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

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.

Thank you!

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?

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

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

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

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

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

Sorry, I have no news..

@DineshNandhagopal, @Davory
Have you found any solutions?