5

I want to display default Google search Results in webview of my Android application. We can do it using Google Custom Search API but it is paid. I want to use Google default search engine to replace the API. Could you please help me to provide its implementation.

Thanks in Advance

Deniz
  • 107
  • 2
  • 15
Vikasdeep Singh
  • 20,983
  • 15
  • 78
  • 104

2 Answers2

8

If I get your question correct, you want to enable the user to enter a search term and then get google results in the webview. If you carefully notice, then in your browser if you enter following URL: https://www.google.com/search?q=query_string, then you will get results for your query of query_string.

So, for your purpose:

Step 1 You can have an EditText and a Button.

Step 2 On Button click you can get the text from an EditText

Step 3 Then Create a URL from the query string that you received from EditText as follows:

String query; // Get the text from EditText here
String url = "https://www.google.com/search?q="+query;

Step 4: Then using that URL you can display the URL result in webview.

The results would be displayed there. There are many thread which will help you achieve this. Some are:

How to get text from EditText?

1) How to get text from EditText?

How to display URL in webview?

1) Trying to display url in Web View

2) To View a web page inside your app?

Your only task is to create the URL and then display is using webview.

Hope it helps. All the best.

Community
  • 1
  • 1
Shobhit Puri
  • 25,769
  • 11
  • 95
  • 124
  • Hi Shobhit, exactly till this part i have already implemented but the problem is that in webview it is displaying Google's default page with search results. I want to remove Google's Edittext and other options like Images, Maps, News etc. but i don't think that is possible. if yes then please let me know... :) – Vikasdeep Singh Jun 07 '13 at 02:32
  • I don't think so it would be possible this way as we are just displaying the webpage which is pointed by that URL. You might need to remove few links and tags which you receive in the HTTP response or something... – Shobhit Puri Jun 07 '13 at 02:35
  • Yes... But its not possible as it is Legally it is not allowed by Google to modify its search results. Anyways thanks a lot – Vikasdeep Singh Jun 07 '13 at 03:07
5

Before loading your search query URL into webView, must enable javascript so that it may behave properly and will not ask to open in the default browser.

String url = "https://www.google.com/search?q="+editText.getText();
webView1.getSettings().setJavaScriptEnabled(true);

webView1.loadUrl(url);

Cheers

Rohit5k2
  • 17,948
  • 8
  • 45
  • 57
Omama
  • 147
  • 1
  • 2
  • 9