Enable Longclick in Webview

Enable longClick in WebView

I had this same problem.

Unfortunately, I could not find a way to make the standard browser menu options appear. You have to implement each one yourself. What I did was to register the WebView for context menus with activity.registerForContextMenu(webView). Then I subclassed the WebView and overrode this method:

@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);
} else if (result.getType() == HitTestResult.ANCHOR_TYPE ||
result.getType() == HitTestResult.SRC_ANCHOR_TYPE) {
// Menu options for a hyperlink.
//set the header title to the link url
menu.setHeaderTitle(result.getExtra());
menu.add(0, ID_SAVELINK, 0, "Save Link").setOnMenuItemClickListener(handler);
menu.add(0, ID_SHARELINK, 0, "Share Link").setOnMenuItemClickListener(handler);
}
}

If you want to do something other than a context menu, then use an OnLongClickListener. However you want to intercept the long click event, the HitTestResult is the key. That's what will allow you to figure out what the user clicked on and do something with it.

I haven't actually implemented "Save Link" myself, I just included it as an example here. But to do so you would have to do all the processing yourself; you'd have to make an HTTP GET request, receive the response, and then store it somewhere on the user's SD card. There is no way that I know of to directly invoke the Browser app's download activity. Your "Save Link" code will look something like this:

HitTestResult result = getHitTestResult();
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet(result.getExtra());
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
if (entity != null) {
URL url = new URL(result.getExtra());

//Grabs the file part of the URL string
String fileName = url.getFile();

//Make sure we are grabbing just the filename
int index = fileName.lastIndexOf("/");
if(index >= 0)
fileName = fileName.substring(index);

//Create a temporary file
File tempFile = new File(Environment.getExternalStorageDirectory(), fileName);
if(!tempFile.exists())
tempFile.createNewFile();

InputStream instream = entity.getContent();
BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
//Read bytes into the buffer
ByteArrayBuffer buffer = new ByteArrayBuffer(50);
int current = 0;
while ((current = bufferedInputStream.read()) != -1) {
buffer.append((byte) current);
}

//Write the buffer to the file
FileOutputStream stream = new FileOutputStream(tempFile);
stream.write(buffer.toByteArray());
stream.close();
}

onLongClick for plain text inside of a WebView

I have got an answer here ,

https://stackoverflow.com/a/5908125/1503130

Also if you have any other fix to the problem then do share it.

How to enable longpress action to download on images in android WebView?

You need to register yout WebView for context menu. In yout activity registerForContextMenu(webView);

and after override onCreateContextMenu method shown bellow

WebView webView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

webView = (WebView)findViewById(R.id.WebView1);

webView.getSettings().setJavaScriptEnabled(true);

webView.setWebViewClient(new WebViewClient());

registerForContextMenu(webView);

webView.loadUrl(HTTP_URL);
}

@Override
public void onCreateContextMenu(ContextMenu contextMenu, View view, ContextMenu.ContextMenuInfo contextMenuInfo){
super.onCreateContextMenu(contextMenu, view, contextMenuInfo);

final WebView.HitTestResult webViewHitTestResult = webView.getHitTestResult();

if (webViewHitTestResult.getType() == WebView.HitTestResult.IMAGE_TYPE ||
webViewHitTestResult.getType() == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE) {

contextMenu.setHeaderTitle("Download Image From Below");

contextMenu.add(0, 1, 0, "Save - Download Image")
.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem menuItem) {

String DownloadImageURL = webViewHitTestResult.getExtra();

if(URLUtil.isValidUrl(DownloadImageURL)){

DownloadManager.Request request = new DownloadManager.Request(Uri.parse(DownloadImageURL));
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
DownloadManager downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
downloadManager.enqueue(request);

Toast.makeText(MainActivity.this,"Image Downloaded Successfully.",Toast.LENGTH_LONG).show();
}
else {
Toast.makeText(MainActivity.this,"Sorry.. Something Went Wrong.",Toast.LENGTH_LONG).show();
}
return false;
}
});
}
}

or this is alternative

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
final WebView.HitTestResult result = browser.getHitTestResult();
MenuItem.OnMenuItemClickListener handler = new MenuItem.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
// handle on context menu click
return true;
}
};

if (result.getType() == WebView.HitTestResult.IMAGE_TYPE ||
result.getType() == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE) {

menu.setHeaderTitle(result.getExtra());
menu.add(0, 666, 0, "Save Image").setOnMenuItemClickListener(handler);
}
}

Horizontal menu inflater on long click for web view

In whichever activity is hosting your WebView, override onActionModeStarted(), manipulate the menu items, and assign listeners to each. An example:

@Override
public void onActionModeStarted(ActionMode mode) {
super.onActionModeStarted(mode);

MenuInflater menuInflater = mode.getMenuInflater();
Menu menu = mode.getMenu();

menu.clear();
menuInflater.inflate(R.menu.menu_custom, menu);

menu.findItem(R.id.custom_one).setOnMenuItemClickListener(new ToastMenuItemListener(this, mode, "One!"));
menu.findItem(R.id.custom_two).setOnMenuItemClickListener(new ToastMenuItemListener(this, mode, "Two!"));
menu.findItem(R.id.custom_three).setOnMenuItemClickListener(new ToastMenuItemListener(this, mode, "Three!"));
}

private static class ToastMenuItemListener implements MenuItem.OnMenuItemClickListener {

private final Context context;
private final ActionMode actionMode;
private final String text;

private ToastMenuItemListener(Context context, ActionMode actionMode, String text) {
this.context = context;
this.actionMode = actionMode;
this.text = text;
}

@Override
public boolean onMenuItemClick(MenuItem item) {
Toast.makeText(context, text, Toast.LENGTH_SHORT).show();
actionMode.finish();
return true;
}
}

Disable long click in android webview

I avoided using GestureDetector and SimpleOnGestureListener, I done it with catching touch listnerby catching the position fron MotionEvent.ACTION_DOWN and MotionEvent.ACTION_UP



Related Topics



Leave a reply



Submit