There is a simple way to implement Google search in your Android app. In this example, we will accept the user input as the search term, and we will use Intent. ACTION_WEB_SEARCH.
GoogleSearchIntentActivity. java
Package com. Alibaba talkative. 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>
</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">
</Button> </linearlayout>
Note: Do not forget to add INTERNET permissions to the AndroidManifest. xml file.
<Uses-permission android: name = "android. permission. INTERNET">
</Uses-permission>
Output: