Tutorial : Google Custom Search in Android app

If you want to search something inside your Android app using Google – this tutorial can be usefull.

Source code of sample app :
https://github.com/fanysoft/Android_Google_Custom_SearchDemo

Note : Use your own Google API key and Search ID and paste them into code to make it work – see below steps.

Video tutorial :

Steps :

Part 1 : Create your API key
a) Access https://console.developers.google.com/iam-admin/projects

b) Click „Create Project“

c) Name it and insert details

d) In dashboard make sure that your new project is selected (if you already have more than 1)

e) Click in section „Getting Started“ to „Enable APIs and get credentials such as keys“

f) Click „Enable APIs and Services“

g) Search for „Custom Search API“ and select it

h) Press „Enable“ and wait for a while once it is enabled

i) Click „Create credentials“

j) Click „What credentials do I need?“ to finally get your API key
example is AIzaSyAG_BE-nHOhE8JpJbyRKUyp9jx6lH-hvbs
Note : dont use this one, but get yours

Part 2 : Create Custom Search Engine

SCREENSHOTS ARE OUTDATED anyway similar to new state

a) access https://cse.google.com/cse/all

b) create new search engine

Screenshot_5

c) add search sites, select language for seach, name it

Screenshot_6

d) under Edit seach engine – Basics – Details – click „Search Engine ID“ to get ID.
example is : 008492847764751849638:qq4bjv_sqfu
Note : dont use this one, but get yours


Screenshot_7


Screenshot_8

Part 3 : Download my Android code
https://github.com/fanysoft/Android_Google_Custom_SearchDemo

Part 4 : Modify code
replace API key andd Search Engine ID within MainActivity.class

// Your API key
// TODO replace with your value
String key="AIzaSyAY4lFujVxjP.........";

// Your Search Engine ID
// TODO replace with your value
String cx = "0084928477647518.........";

Part 5 : Run the app

Note : result from Google Search server is in JSON format – example is here. To futher parse it, see https://stackoverflow.com.

Napsat komentář