I have successfully gotten my action bar to show up right in android. I am now trying to implement the search. I am trying to start small and at least see if I can get the search term before I try and do anything with it.
I followed this tutorial here:
http://developer.android.com/training/search/setup.html
and created a SearchResultsActivity class which looks like this to handle the search.
import android.app.Activity;
import android.app.SearchManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;
public class SearchResultsActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
//possible more code
handleIntent(getIntent());
}
@Override
protected void onNewIntent(Intent intent) {
//possible more code
handleIntent(intent);
}
private void handleIntent(Intent intent) {
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
//use the query to search your data somehow
//toast error test
Context context = getApplicationContext();
CharSequence text = "Hello toast!";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, query, duration);
toast.show();
}
}
///more code
}
I tried to add a toast to see if when I entered text then clicked search if a toast would pop up. Right now no toast shows up.
I am working on a similar project with a search widget in the action bar. This is only compatible with API 11+ so no gingerbread, but it looks nicer. You still have to set up a searchable.xml and add the appropriate meta-data in the manifest. I'm not sure how you are going to implement this, but the code below loads a ListView, then adds the string. The onCreateOptionsMenu() section configures the search widget and filters the ListView based on the imputed text. Hope it helps!
public class ListOfMathCourses extends ListActivity {
ArrayAdapter<String> adapter;
ListView list;
String[] math = new String[] {"Pre-Algebra", "Algebra I", "Algebra II", "Geometry", "Calculus"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.startingpoint);
list = (ListView) findViewById(android.R.id.list);
adapter = new ArrayAdapter<String>(this, R.layout.listviewrow, math);
list.setAdapter(adapter);
getListView().setTextFilterEnabled(true);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_search, menu);
//getMenuInflater().inflate(R.menu.action, menu);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false);
SearchView.OnQueryTextListener queryTextListener = new SearchView.OnQueryTextListener()
{
@Override
public boolean onQueryTextChange(String newText)
{
// this is your adapter that will be filtered
adapter.getFilter().filter(newText);
return true;
}
@Override
public boolean onQueryTextSubmit(String query)
{
// this is your adapter that will be filtered
adapter.getFilter().filter(query);
return true;
}
};
searchView.setOnQueryTextListener(queryTextListener);
return super.onCreateOptionsMenu(menu);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With