Breaking News

How to copy link address using context menu with android Web view




In this post, we again create a context menu like the previous download image button, now we use the copy image address button with that.

For that, we add a new button to copy the image address in our context menu.

MainActivity.java
 @Override  
   public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {  
     super.onCreateContextMenu(menu, v, menuInfo);  
     final WebView.HitTestResult webviewHittestResult = mywebview.getHitTestResult();  
     DownloadImageUrl = webviewHittestResult.getExtra();  
     if(webviewHittestResult.getType() == WebView.HitTestResult.IMAGE_TYPE ||  
         webviewHittestResult.getType() == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE)  
     {  
       if(URLUtil.isNetworkUrl(DownloadImageUrl))  
       {  
         menu.setHeaderTitle("Download Image from Below");  
         menu.add(0,2,0,"Copy Image Address").setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {  
           @Override  
           public boolean onMenuItemClick(MenuItem menuItem) {  
             String copyimageurl = webviewHittestResult.getExtra();  
             ClipboardManager manager = (ClipboardManager)getSystemService(Context.CLIPBOARD_SERVICE);  
             ClipData clip = ClipData.newPlainText("label",copyimageurl);  
             manager.setPrimaryClip(clip);  
             Toast.makeText(MainActivity.this, "Copied to Clipboard", Toast.LENGTH_SHORT).show();  
             return false;  
           }  
         });  
       }  
     }  
     else  
     {  
       Toast.makeText(this, "Error", Toast.LENGTH_SHORT).show();  
     }  
   }  

Here, we add a button called copy image address, and in that, we call a clipboard manager to copy the image address to the clipboard.

Next, we create a Share image to another app from webview context menu.
Follow us for more posts like this, 
Subscribe Harpreet studio on Youtube  
Like Harpreet Studio on Facebook  
Follow me on Instagram 

No comments