Android WebView inside ListView onclick event issues
Asked Answered
O

6

8

I have a ListView where each row has two webviews side by side, taking up the entire row. I've set up onListItemClick() in my ListActivity, but they are not fired when I tap on one of the rows (unless the place I happen to tap is outside the webview's border - but this isn't likely behavior, users would most likely want to tap on the image inside the webview).

I've tried setting setFocusable(false) and setFocusableInTouchMode(false), but those don't help.

Ideally this would operate exactly like Facebook's newsfeed, where you can tap on the profile picture of someone's wall post and it acts as if you've tapped the entire row (in FBs case the text for the wall post)

Oust answered 11/2, 2011 at 19:34 Comment(0)
O
23

Figured it out, posting my solution in case someone else wants to do something similar:

I had to use an OnTouchListener, since OnClick and OnFocus weren't working. I extended a class that is reuseable:

private class WebViewClickListener implements View.OnTouchListener {
    private int position;
    private ViewGroup vg;
    private WebView wv;

    public WebViewClickListener(WebView wv, ViewGroup vg, int position) {
        this.vg = vg;
        this.position = position;
        this.wv = wv;
    }

    public boolean onTouch(View v, MotionEvent event) {
        int action = event.getAction();

        switch (action) {
            case MotionEvent.ACTION_CANCEL:
                return true;
            case MotionEvent.ACTION_UP:
                sendClick();
                return true;
        }

        return false;
    }

    public void sendClick() {
        ListView lv = (ListView) vg;
        lv.performItemClick(wv, position, 0);
    }
}

The sendClick method could be overridden to do what's needed in your specific case. Use case:

WebView image = (WebView) findViewById(R.id.myImage);
image.setOnTouchListener(new WebViewClickListener(image, parent, position));
Oust answered 11/2, 2011 at 20:19 Comment(2)
i tried this.it didn't worked for me. thanks for any other idea.Gallous
This works like it should! Thanks!. You might use it with set and get Tag in some real life cases.Horbal
F
7

I managed to get this working with the following:

class NoClickWebView extends WebView {
    public NoClickWebView(Context context) {
        super(context);
        setClickable(false);
        setLongClickable(false);
        setFocusable(false);
        setFocusableInTouchMode(false);
    }
}

You can use this class or just set these properties on a standard WebView.

Formate answered 4/3, 2012 at 19:33 Comment(0)
I
6

Got it working in Android 4.4 using the same approach as bjdodson above but also overriding dispatchTouchEvent

public class NonClickableWebview extends WebView {
    public NonClickableWebview(Context context, AttributeSet attrs) {
        super(context, attrs);
        setClickable(false);
        setLongClickable(false);
        setFocusable(false);
        setFocusableInTouchMode(false);
    }
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        return false;
    }
}
Inconvincible answered 23/11, 2014 at 12:28 Comment(2)
thanks. its worked for me after trying all the above solutions.Gallous
This solution works well, whereas the others seemed to be quite a bit more work. Note that in "modern" Android Studio-ish techniques, the easiest way to accomplish this is to add a new class file with this code, create a regular WebView in your XML layout file, then edit the XML, replacing WebView with com.myapp.NonClickableWebview After that, your code is something like (NonClickableWebview) myView = (NonClickableWebview) findViewById( R.id.myView ); but you can otherwise address it as a standard WebViewIo
H
2

I just tried what is suggested in this post WebView inside the Custom ListView: Click Listener is not Working, I dunno why but if you set these features in the XML they don't seem to work. Doing this dynamically does allow you to click on the listview item =)

Hysterics answered 14/6, 2011 at 23:13 Comment(1)
Currently getting a 404 for this link.Countryandwestern
T
2

Setup to parent layout of each WebView:

android:descendantFocusability="blocksDescendants" 
android:layout_width="match_parent"
android:layout_height="wrap_content"
Toxicology answered 13/6, 2013 at 8:5 Comment(0)
W
1

I was using the following layout in ListView and it was working perfectly, ie its clicking, scrolling and so on.

TextView1 
ImageView1 TextView2 ImageView2
TextView3

Then I have changed the layout with following, ie I have added with WebView control in left most corner in place of ImageView1

TextView1 
WebView TextView2 ImageView2
TextView3

After doing this, clicking was not working for me.

I solve this problem by adding this to the Webview:

webView.setFocusable(false);
webView.setClickable(false);
Warga answered 10/1, 2013 at 19:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.