Search code examples
androidwebviewyoutube

how to get video id from clicking YouTube search page link in a webview


I have a webview in which I present YouTube search page. (https://www.youtube.com/results?search_query=) I want to receive the video id of the clip the user pressed. so far I have tried the following without success:

  1. use of shouldOverrideUrlLoading - isn't being called when pressing on the video clip in search
    public WebViewClient getWebViewClient()
    {
        return new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                Log.d("TAG","called overide url");
                if (url.startsWith(YOUTUBE_VIDEO_URL)) {
                    setVideoIdFromLink(url);

                    return true;
                }
                else
                {
                    Log.d("GABI","url:"+url);
                }
                return false;
            }
            /*
            public void onLoadResource (WebView view, String url){

                if(url.startsWith(YOUTUBE_VIDEO_URL)){
                    setVideoIdFromLink(url);
                }
            }*/
        };
    }
  1. setting an onTouchListener - method always return type 0 extras null
    private OnTouchListener linkClickTouch()
    {
        return new OnTouchListener() {
            @Override
            public boolean onTouch(View v, 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) {
                            v.performClick();
                        }
                    }
                }
                return true;
            }
            };
        }

the click action:

private OnClickListener linkClick()
    {
        
        return new OnClickListener()
        {
            public void onClick(View v) {

                
                final WebView webview = (WebView) v;
                final WebView.HitTestResult result = webview.getHitTestResult();
                Log.d("TAG", "result:"+result.getType()+"\nextras:"+
                result.getExtra());
                
                
                if (result.getType() == WebView.HitTestResult.SRC_ANCHOR_TYPE) {
                    
                }

                if (result.getType() == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE) {
                
                }

                if (result.getType() == WebView.HitTestResult.IMAGE_TYPE) {
                
                }

                
            }
        };
    }

I know I can probably use the YouTube API but I want to accomplish that without building a search activity which will display results only to find the id, when a decent search page already exists.


Solution

  • after many trial and error: shouldOverrideUrlLoading or onPageStarted never gets called.

    you should use -

    public void onLoadResource (WebView view, String url)
    {
         if(url.startsWith("https://m.youtube.com/watch?"))
         {
    
             setVideoIdFromLink(url);
             view.stopLoading();
         }
    }
    
    public String setVideoIdFromLink(String url)
    {
        //get the video id from the link
        String[] args=url.split("v=");
        String videoId=args[args.length-1];
    
        return videoId;
    }