Skip to content

Instantly share code, notes, and snippets.

@mderazon
Last active December 23, 2015 15:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mderazon/6654048 to your computer and use it in GitHub Desktop.
Save mderazon/6654048 to your computer and use it in GitHub Desktop.
how to put the search box in the actionbar
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// ......
// ......
ActionBar actionBar = getSupportActionBar(); // you can use ABS or the non-bc ActionBar
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_USE_LOGO | ActionBar.DISPLAY_SHOW_HOME
| ActionBar.DISPLAY_HOME_AS_UP); // what's mainly important here is DISPLAY_SHOW_CUSTOM. the rest is optional
LayoutInflater inflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// inflate the view that we created before
View v = inflater.inflate(R.layout.actionbar_search, null);
AutoCompleteTextView textView = (AutoCompleteTextView) v.findViewById(R.id.search_box);
textView.setAdapter(searchAdapter);
textView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// do something when the user clicks
}
});
actionBar.setCustomView(v);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment