2

Im just a beginner to android so had a doubt on if this is possible or not .

In my application i need to pass a string value into youtube site's search bar and the perform a search .

Instead of manually entering it into the Webview , can this be passed via code ?

Can this be made possible ?

Titus Saju
  • 89
  • 2
  • 9

1 Answers1

0

There are two ways to do so, the first one is just passing the search query as a get parameter.

String query = "never gonna give you up";
webview.loadUrl("https://www.youtube.com/results?search_query=" + query);

The second, which might be more elegant would passing JS code that will preform the change.

webview.loadUrl("https://www.youtube.com");
// wait for it to load
webview.loadUrl("javascript:(function () { " + "<js code>" + "})()");
Kirill Kulakov
  • 10,035
  • 9
  • 50
  • 67
  • 1
    But usually , for eg i query for "taylor swift" , i get a youtube url of . – Titus Saju Mar 23 '16 at 17:54
  • https://www.youtube.com/results?search_query=taylor+swift How do you propose that i add in the "+" symbol instead of the space ? – Titus Saju Mar 23 '16 at 17:54
  • agrTextView.getText().toString().replaceAll(" ", "+"); will do the work right ?? – Titus Saju Mar 23 '16 at 17:56
  • Okay , New task . In the webview as of now i get the result of my search. Just assume that i have clicked on one of the result's . How do i setup the activity to respond in such a manner that it has to listen to my click on the webview .. – Titus Saju Mar 23 '16 at 18:03
  • For eg: I choose a search result , which directs me to the Video player page . , Instead i need to just get the url of that page passed into the activity back .. Is that possible ? – Titus Saju Mar 23 '16 at 18:05
  • Can we implement such a Listener? – Titus Saju Mar 23 '16 at 18:06
  • you pass JS code that will perform the thing you want to do in the webview, and pass the data or the callback to your java code. see http://stackoverflow.com/questions/3298597/how-to-get-return-value-from-javascript-in-webview-of-android/19077097#19077097 – Kirill Kulakov Mar 23 '16 at 18:12