Example of Google search in Android apps

Source: Internet
Author: User

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:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.