Web View Detect On Touch Only When No Link Is Clicked Android
I load static htmls & urls in webview. Also, my web view is clickable ,on its click intent is fired. I want to implement- when the link in url/html is clicked in webview li
Solution 1:
What is happening here is when you assign ontouchlistener to the whole webview other elements go under it and they dont get any touch or click. To solve this problem you can do following
public class MyWebViewClicker extends Activity implements OnTouchListener, Handler.Callback {
private static final int CLICK_WEBVIEW = 1;
private static final int CLICK_URL = 2;
private final Handler handler = new Handler(this);
private WebView webView;
private WebViewClient webviewclient;
boolean donext=false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.web_view);
webView = (WebView)findViewById(R.id.myweb);
webView.setOnTouchListener(this);
webviewclient = new WebViewClient(){
@Override public boolean shouldOverrideUrlLoading(WebView view, String url) {
handler.sendEmptyMessage(CLICK_URL);
return false;
}
};
webView.setWebViewClient(webviewclient);
webView.setVerticalScrollBarEnabled(false);
webView.loadUrl("http://www.example.com");
}
@Override
public boolean onTouch(View v, MotionEvent event) {
if (v.getId() == R.id.myweb && event.getAction() == MotionEvent.ACTION_DOWN) {
handler.sendEmptyMessageDelayed(CLICK_WEBVIEW, 500);
}
return false;
}
@Override
public boolean handleMessage(Message msg) {
if (msg.what == CLICK_URL){
Toast.makeText(this, "URL is clicked", Toast.LENGTH_SHORT).show();
donext=true;
return true;
}
if (msg.what == CLICK_WEBVIEW){
if(donext==true)
{
//do nothing
}else{
Toast.makeText(this, "WebView is clicked", Toast.LENGTH_SHORT).show();
}
return true;
}
return false;
}
}
Post a Comment for "Web View Detect On Touch Only When No Link Is Clicked Android"