0

i am trying to make my webview bring an option to save the current image when the image is long tapped i have tried the below code outside the oncreate but it does not do anything at all!

 public boolean onLongClick(View v) {
     openContextMenu(v);
     return true;
 }
 @Override
 public void onCreateContextMenu(ContextMenu menu, View v,
                     ContextMenu.ContextMenuInfo menuInfo) {
     super.onCreateContextMenu(menu, v, menuInfo);
     MenuInflater inflater = getMenuInflater();
     inflater.inflate(R.menu.context, menu);
 }
 @Override
 public boolean onContextItemSelected(MenuItem item) {
   AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
   switch (item.getItemId()) {
   case R.id.save_image:
       Toast.makeText(this, "save failed",
             Toast.LENGTH_LONG).show();
       return true;
   default:
     return super.onContextItemSelected(item);
   }
 }
JRE.exe
  • 801
  • 1
  • 15
  • 28

1 Answers1

1
@Override
protected void onCreateContextMenu(ContextMenu menu) {
    super.onCreateContextMenu(menu);

    HitTestResult result = getHitTestResult();

    MenuItem.OnMenuItemClickListener handler = new MenuItem.OnMenuItemClickListener() {
        public boolean onMenuItemClick(MenuItem item) {
                // do the menu action
                return true;
        }
    };

    if (result.getType() == HitTestResult.IMAGE_TYPE ||
            result.getType() == HitTestResult.SRC_IMAGE_ANCHOR_TYPE) {
        // Menu options for an image.
        //set the header title to the image url
        menu.setHeaderTitle(result.getExtra());
        menu.add(0, ID_SAVEIMAGE, 0, "Save Image").setOnMenuItemClickListener(handler);
        menu.add(0, ID_VIEWIMAGE, 0, "View Image").setOnMenuItemClickListener(handler);
    }
}
Ayaanp
  • 327
  • 1
  • 12
  • where should i put this code? inside or outside oncreate? – JRE.exe Jan 29 '15 at 12:05
  • also your code is not error free please consider explaining the full implementation. thanks – JRE.exe Jan 29 '15 at 12:10
  • First register the WebView for context menus like so: activity.registerForContextMenu(webView) To save you images simply use this [link](http://stackoverflow.com/questions/3474448/saving-image-webview-android/3475772#3475772) – Ayaanp Jan 29 '15 at 12:11
  • i dont know how to do that as i am very new to android thanks for understanding – JRE.exe Jan 29 '15 at 12:22