Search from the action bar

I successfully got my action bar to display right in android. Now I'm trying to do a search. I'm trying to start small and at least see if I can get a search query before I try to do anything with it.

I followed this tutorial here:

http://developer.android.com/training/search/setup.html

and created a SearchResultsActivity class that looks like it handles 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 adding a toast to see when I entered the text, then clicked search if a toast appears. There are no toasts right now.

+4
source share
1 answer

I am working on a similar project with a search widget in the action bar. This is only compatible with API 11+, so there are no gingerbread cookies, but it looks better. You still need to tweak the searchable.xml file and add the appropriate metadata to the manifest. I'm not sure how you are going to implement this, but the code below loads a ListView and then adds a row. The onCreateOptionsMenu () section customizes the search widget and filters the ListView based on the imputed text. Hope this 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); } 

}

+15
source

Source: https://habr.com/ru/post/1483688/


All Articles