0

I use this code to override webview Context Action Bar but during longclick the CAB appears but no text selection how can i only fire CAB when text selection mode?

   public class MainActivity extends Activity {
   ActionMode.Callback mCallback;
   ActionMode mMode;
   WebView webview;
   @Override
   protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    webview=webview = (WebView) findViewById(R.id.webView1);
    webview.loadUrl("http://www.google.com");


    webview.setOnLongClickListener(new OnLongClickListener() {
        @TargetApi(Build.VERSION_CODES.HONEYCOMB)
        @SuppressLint("NewApi")
        @Override
        public boolean onLongClick(View v) {
            // TODO Auto-generated method stub
            if (mMode != null)
                return false;
            else
                mMode = startActionMode(mCallback);
            return true;

        }
    });



    mCallback = new ActionMode.Callback() {
        @Override
        public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
            return false;
        }

        @Override
        public void onDestroyActionMode(ActionMode mode) {
            mMode = null;
        }

        @SuppressLint("NewApi")
        @Override
        public boolean onCreateActionMode(ActionMode mode, Menu menu) {
            mode.setTitle("1 selected");
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }

        @SuppressLint("NewApi")
        @Override
        public boolean onActionItemClicked(ActionMode mode, MenuItem item) {

            switch (item.getItemId()) {
            case R.id.action_settings:
                Toast.makeText(getBaseContext(), "Selected Action1 ",
                        Toast.LENGTH_LONG).show();
                mode.finish();
                break;

            }
            return false;
           }

         };
       }

      }

during long press my custom CAB appear without text selection how to make it appear only in the case of text selection? many thanks

1 Answers1

0

don't do this,it is not necessary to overwrite longClick, you should make your custom webview and do some thing that below link display:

Look At This Answer

Community
  • 1
  • 1
MHP
  • 2,613
  • 1
  • 16
  • 26