Search code examples
androidwebviewandroid-actionbaroverridingcontextual-action-bar

Android Webview: Override Contextual Action Bar


I am trying to override a contextual action bar in Android Webview. When I long click on a selected word, the custom action bar is displayed. However, when I click on the action bar button, nothing happens.

It seems like the onContextItemSelected() function is not called. This is my code:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.dictionary);
   mWebView = (WebView) findViewById(R.id.wv);
}

@Override
public boolean onContextItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.save_word:
            Toast.makeText(CheckDictionary.this,"Save Word Meaning Successfully",Toast.LENGTH_LONG).show();
            break;
        default:
            break;
    }
    if (mActionMode != null) {
        mActionMode.finish();
    }
    return super.onContextItemSelected(item);
}

@Override
public void onActionModeStarted(ActionMode mode) {
    if (mActionMode == null) {
        mActionMode = mode;
        mode.setTitle("Save Word Meaning");
        Menu menu = mode.getMenu();
        menu.clear();
        mode.getMenuInflater().inflate(R.menu.dictionary_menu, menu);
    }
    super.onActionModeStarted(mode);
}

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

Solution

  • I am doing this job on my old projects like this:

     private String[] data = {"1", "2", "3", "4", "5", "6", "7", "8", "9","10"};
    
     private SelectionAdapter mAdapter;
    
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);
    
         mAdapter = new SelectionAdapter(this,
                     R.layout.row_list_item, R.id.textView1, data);
         setListAdapter(mAdapter);
         getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
    
         getListView().setMultiChoiceModeListener(new MultiChoiceModeListener() {
    
             private int nr = 0;
    
             @Override
             public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
                 // TODO Auto-generated method stub
                 return false;
             }
    
             @Override
             public void onDestroyActionMode(ActionMode mode) {
                 // TODO Auto-generated method stub
                  mAdapter.clearSelection();
             }
    
             @Override
             public boolean onCreateActionMode(ActionMode mode, Menu menu) {
                 // TODO Auto-generated method stub
    
                 nr = 0;
                 MenuInflater inflater = getMenuInflater();
                 inflater.inflate(R.menu.contextual_menu, menu);
                 return true;
             }
    
             @Override
             public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
                 // TODO Auto-generated method stub
                 switch (item.getItemId()) {
    
                     case R.id.item_delete:
                         nr = 0;
                         mAdapter.clearSelection();
                         mode.finish();
                 }
                return false;
             }
    
             @Override
             public void onItemCheckedStateChanged(ActionMode mode, int position,
                     long id, boolean checked) {
                 // TODO Auto-generated method stub
                  if (checked) {
                         nr++;
                         mAdapter.setNewSelection(position, checked);                    
                     } else {
                         nr--;
                         mAdapter.removeSelection(position);                 
                     }
                     mode.setTitle(nr + " selected");
    
             }
         });
    
         getListView().setOnItemLongClickListener(new OnItemLongClickListener() {
    
             @Override
             public boolean onItemLongClick(AdapterView<? arg0, View arg1,
                     int position, long arg3) {
                 // TODO Auto-generated method stub
    
                 getListView().setItemChecked(position, !mAdapter.isPositionChecked(position));
                 return false;
             }
         });
     }
    
     private class SelectionAdapter extends ArrayAdapter<String {
    
         private HashMap<Integer, Boolean mSelection = new HashMap<Integer, Boolean();
    
         public SelectionAdapter(Context context, int resource,
                 int textViewResourceId, String[] objects) {
             super(context, resource, textViewResourceId, objects);
         }
    
         public void setNewSelection(int position, boolean value) {
             mSelection.put(position, value);
             notifyDataSetChanged();
         }
    
         public boolean isPositionChecked(int position) {
             Boolean result = mSelection.get(position);
             return result == null ? false : result;
         }
    
         public Set<Integer getCurrentCheckedPosition() {
             return mSelection.keySet();
         }
    
         public void removeSelection(int position) {
             mSelection.remove(position);
             notifyDataSetChanged();
         }
    
         public void clearSelection() {
             mSelection = new HashMap<Integer, Boolean();
             notifyDataSetChanged();
         }
    
         @Override
         public View getView(int position, View convertView, ViewGroup parent) {
             View v = super.getView(position, convertView, parent);//let the adapter handle setting up the row views
             v.setBackgroundColor(getResources().getColor(android.R.color.background_light));
    
    
             if (mSelection.get(position) != null) {
                 v.setBackgroundColor(getResources().getColor(android.R.color.holo_blue_light);
    
             }
             return v;
         }
     } 
    

    You can make changes in this code according to your needs...