2

I have a WebView in Form. And I am using this code.

WebView ww = (WebView)findViewById(R.id.webView);
WebSettings webSettings = ww.getSettings();
webSettings.setJavaScriptEnabled(true);
ww.loadUrl("http://www.youtube.com");

What I want is, when the page has finished loading, I want the my TextView value to be set on the search input box and start a search.

Firstly I think this code will work.

webView.loadUrl("javascript: {document.getElementsByID('masthead-search-term').value ='TEST1';};");

But when I use this code, I see only "TEST1" in all page.

Thanks for help from now!

3 Answers 3

1

I assume textView1 to be the TextView where your query is.

Try the following code,

// Load the query to the search input
String query = textView1.getText().toString();
webView.loadUrl("javascript: {document.getElementById('masthead-search-term').value ='" + query + "';};");

// For the click
webView.loadUrl("javascript: {document.getElementById('search-btn').click();};");
Sign up to request clarification or add additional context in comments.

Comments

0
webView.loadUrl("javascript: {document.getElementsByID('masthead-search').value ='TEST1';};");

I checked youtube's page and saw youtube's Search bar id is masthead-search. Secondly, If you run that it will just write TEST1 in Search bar.

I did not understand your question correctly, pardon me for that however, I guess you asked whether it is possible to click that search button.

webView.loadUrl("javascript: {document.getElementsByID('masthead-search').value ='TEST1'.click();};");

I am not 100% sure. It's a try and error method. Cheers!

Comments

0

Looks like browser inserts into document value, that was returned by javascript: code in search bar. To avoid that you can wrap your call into function, like this:

javascript:(function(){document.getElementById('masthead-search-term').value ='TEST1';document.getElementById('masthead-search').submit();})()

Also pay attention, that it's getElementById(), not getElementsByID()

P.S. It is worth noting that you can achieve the same effect by just redirecting user to https://www.youtube.com/results?search_query=test1 url.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.