I have Android LisView that was contain TextView to display the data in the list,
I add to change it to Webview, after doing that everything look good except the setOnClickListener that not responding anymore..
I have read about the Webview and found that setOnClickListener is not supported, instead setOnTouchListener is supported is there
a way to use the same functionality as setOnClickListener in Android WebView ?
Like this:
myWebView.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
//do it ..
}
});
Thanks (:
You need to put the setOnClickListener in one of the activity callbacks. In your onCreate() method, move the button there and then setOnClickListener() . Show activity on this post.
setOnClickListener(this); means that you want to assign listener for your Button “on this instance” this instance represents OnClickListener and for this reason your class have to implement that interface. If you have more than one button click event, you can use switch case to identify which button is clicked.
In Android, the OnClickListener() interface has an onClick(View v) method that is called when the view (component) is clicked. The code for a component's functionality is written inside this method, and the listener is set using the setOnClickListener() method.
One of the most usable methods in android is setOnClickListener method which helps us to link a listener with certain attributes. While invoking this method a callback function will run. One can also create a class for more than one listener, so this can lead you to code reusability.
I ended up with this solution:
public class ClickableWebView extends WebView {
private static final int MAX_CLICK_DURATION = 200;
private long startClickTime;
public ClickableWebView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public ClickableWebView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public ClickableWebView(Context context) {
super(context);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: {
startClickTime = Calendar.getInstance().getTimeInMillis();
break;
}
case MotionEvent.ACTION_UP: {
long clickDuration = Calendar.getInstance().getTimeInMillis() - startClickTime;
if(clickDuration < MAX_CLICK_DURATION) {
super.performClick();
}
}
}
return true;
}
}
Remarks:
WebView
(e.g.: hyperlinks)OnClickListener
by adding an onClick
in xml or in JavaThanks to Stimsoni Answer to How to distinguish between move and click in onTouchEvent()?
Why not use onTouch listener as you stated?
myWebView.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
return false;
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With