The Cache: Technology Expert's Forum
 
*
Welcome, Guest. Please login or register. August 20, 2019, 02:38:30 AM

Login with username, password and session length


Pages: [1]
  Print  
Author Topic: Tutorial: How to use Androids built in Search UI  (Read 6270 times)
deregular
Expert
****
Offline Offline

Posts: 172


View Profile
« on: March 28, 2011, 11:44:14 PM »

This took a bit for me to figure out, but posting here as its probably the quickest tute I could think of for the new Android board.

Ive attached an image of what the wiki one looks like.

1) In your .Main Activity you need to set the OptionsMenu (if you want to trigger it from that) so add the 'onCreateOptionsMenu' and 'onOptionsItemSelected' methods.
The 'onSearchRequested()' method is the important part that triggers the search UI.

Code:
private static final int SEARCH = Menu.FIRST;

@Override
public boolean onCreateOptionsMenu(Menu menu) {
        menu.add(0, SEARCH, SEARCH, "Search").setIcon(R.drawable.ic_search_icon);
        return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
        switch(item.getItemId()) {
                case SEARCH:
                        onSearchRequested();
                        return true;
        }
        return super.onOptionsItemSelected(item);
}

2) Next you want to let it know which activity will take the search result and do something with it.

So in your manifest file...

Code:
<activity android:name=".Main"
android:label="@string/app_name">
<meta-data android:name="android.app.default_searchable" android:value=".SearchFor" />
    <intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

<activity android:name=".SearchFor" android:label="@string/title_searchonline">
<intent-filter>
    <action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data android:name="android.app.searchable" android:resource="@xml/searchable"/>
</activity>

3) Now we need to get results from the search box so that we can search something with it.
On the receiving activity that was defined in your manifest...

Code:
public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.searchtabs);

        Intent intent = getIntent();
        if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
                String query = intent.getStringExtra(SearchManager.QUERY);
   
                //Do stuff here
   
        }
}

There are probably some errors in here, as I copy/pasted from my own app code. But it should help anyone looking to do this get the general idea of how it works..


* images.jpg (11.09 KB, 183x275 - viewed 2365 times.)
Logged
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #1 on: March 29, 2011, 12:16:26 AM »

This is going to be great Dereg, thanks for the post.

I wish I had the time to learn Objective C and compare/contrast.
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
EmaEsabel
n00b
*
Offline Offline

Posts: 1



View Profile
« Reply #2 on: October 10, 2011, 05:57:15 AM »

I am also still struggling to learn this.
Logged

No links in sigs.
jackdiaz
n00b
*
Offline Offline

Posts: 1



View Profile
« Reply #3 on: December 16, 2011, 05:48:34 AM »

Its too difficult for me to learn all this coding stuff, but em still struggling to learn it.
Logged

Pages: [1]
  Print  
 
Jump to:  

Perkiset's Place Home   Best of The Cache   phpMyIDE: MySQL Stored Procedures, Functions & Triggers
Politics @ Perkiset's   Pinkhat's Perspective   
cache
mart
coder
programmers
ajax
php
javascript
Powered by MySQL Powered by PHP Powered by SMF 1.1.2 | SMF © 2006-2007, Simple Machines LLC
Seo4Smf v0.2 © Webmaster's Talks


Valid XHTML 1.0! Valid CSS!