Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Start Google search query from activity - Android

Tags:

I was wondering if there is an easier way (or any way) to start a Browser with a Google search query. For example user can select a certain word or phrase and click a button and the activity will start the browser with the Google search query.

Thank you.

like image 586
madu Avatar asked Jan 26 '11 01:01

madu


3 Answers

The Intent class defines an action specifically for web searches:

http://developer.android.com/reference/android/content/Intent.html#ACTION_WEB_SEARCH

Here's an example of how to use it:

Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);
intent.putExtra(SearchManager.QUERY, query); // query contains search string
startActivity(intent);
like image 60
zen_of_kermit Avatar answered Sep 28 '22 08:09

zen_of_kermit


You can do this quite easily with a few lines of code (assuming you want to search Google for 'fish'):

String escapedQuery = URLEncoder.encode(query, "UTF-8");
Uri uri = Uri.parse("http://www.google.com/#q=" + escapedQuery);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);

Otherwise, if you would rather start up your own Activity to handle the browsing, you should be able to do so with a WebView: http://developer.android.com/reference/android/webkit/WebView.html

I think the better answer here is @zen_of_kermit's. It would be nice though, if Android allowed a user to provide the Search engine has an extra though for the ACTION_WEB_SEARCH, rather than just using Google.

like image 40
nicholas.hauschild Avatar answered Sep 28 '22 08:09

nicholas.hauschild


the # gave me trouble:

Uri uri = Uri.parse("https://www.google.com/search?q="+query);
Intent gSearchIntent = new Intent(Intent.ACTION_VIEW, uri);
activity.startActivity(gSearchIntent);
like image 35
Oded Breiner Avatar answered Sep 28 '22 09:09

Oded Breiner