In Android application, if there is functionality of implementing Google search then here is the easy solution to implement it. In this example, we will accept input from the user and will search the same string input in the Google search. For implementing this functionality we use Intent.ACTION_WEB_SEARCH .
Solution:
GoogleSearchIntentActivity.java
package com.technotalkative.googlesearchintent; import android.app.Activity; import android.app.SearchManager; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.EditText; public class GoogleSearchIntentActivity extends Activity { private EditText editTextInput; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); editTextInput = (EditText) findViewById(R.id.editTextInput); } public void onSearchClick(View v) { try { Intent intent = new Intent(Intent.ACTION_WEB_SEARCH); String term = editTextInput.getText().toString(); intent.putExtra(SearchManager.QUERY, term); startActivity(intent); } catch (Exception e) { // TODO: handle exception } } }
main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:padding="10dp" > <EditText android:id="@+id/editTextInput" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Enter search text"> <requestFocus /> </EditText> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Search" android:layout_gravity="center" android:onClick="onSearchClick" android:layout_marginTop="10dp" /> </LinearLayout>
Note:
Don’t forget to add INTERNET permission inside the AndroidManifest.xml file.
<uses-permission android:name="android.permission.INTERNET"/>
Download full source code of this example: Android – Implementation of Google Search Intent