Ramz
Ramz

Reputation: 7164

Triger an action when clicking an image in a webview android?

i have a webview with some html data is displayed , which contain some images and hyper link everthing works fine ,what i need when i click on images in the webview it should start an activity.i find a way to detect images in the web view using

WebView.HitTestResult

by using this i can detect the images in a web view ,i put this in the ontouchLisner i got the detection of the image the problem is when i scroll the web view ,if i accidentally moves the finger across the image the activity will launch it is due to the ontouchLisner is there any way to solve this issue the activity should only trigger when i click the image in the web view.

The on touchlisner i used in my code

    wv.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View arg0, MotionEvent arg1) {
            // TODO Auto-generated method stub
            WebView.HitTestResult hr = ((WebView) arg0).getHitTestResult();

            switch (arg1.getAction() & MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_DOWN:

                break;

            case MotionEvent.ACTION_UP:


                    if (hr.getType() == 5 || hr.getType() == 8) {
                        //Detect image in webview
                        startActivity(new
                         Intent(MainActivity.this,Other.class));



                }
                break;

            case MotionEvent.ACTION_POINTER_DOWN:
                Log.d("-------", "clcik.den");

            case MotionEvent.ACTION_POINTER_UP:
                Log.d("-------", "clcik.up");
                break;

            case MotionEvent.ACTION_MOVE:

                Log.d("-------", "clcik.movee"+hr.getType());
                break;
            }

            return false;
        }
    });

Upvotes: 1

Views: 647

Answers (1)

Sats
Sats

Reputation: 885

You need to open a context menu , when you click on image in a webview . For this create a custom webview and override its onCreateContextMenu method. So whenever you touch on image it will open a menu item and you implement your logic on that click. use this code might help you :

    public class CustomWebview extends WebView {
    public static final int ID_DO_SOMETHING = 1;


    private Context ctx;
    public CustomWebview(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
        this.ctx = context; 
    }

    public CustomWebview(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
        this.ctx = context;

    }

    public CustomWebview(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        // TODO Auto-generated constructor stub
        this.ctx = context;

    }

    @Override
    protected void onCreateContextMenu(ContextMenu menu) {
        super.onCreateContextMenu(menu);
        final HitTestResult result = getHitTestResult();
        MenuItem.OnMenuItemClickListener handler = new MenuItem.OnMenuItemClickListener() {
            public boolean onMenuItemClick(MenuItem item) {
                // do the menu action
                switch (item.getItemId()) {
                case ID_DO_SOMETHING:

                    // implement your logic here;

                    break;

                default:
                    break;
                }
                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_DO_SOMETHING, 0, "Your Method Name").setOnMenuItemClickListener(handler);

        }

    }

}

Upvotes: 1

Related Questions