Last active
March 13, 2016 01:53
-
-
Save mderazon/6749699 to your computer and use it in GitHub Desktop.
a main activity with the new ClearableAutoCompleteTextView and a method to toggle between the visibility of the the search icon and the search box
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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); | |
// the view that contains the search "magnifier" icon | |
final ImageView searchIcon = (ImageView) v.findViewById(R.id.search_icon); | |
// the view that contains the new clearable autocomplete text view | |
final ClearableAutoCompleteTextView searchBox = (ClearableAutoCompleteTextView) v.findViewById(R.id.search_box); | |
// start with the text view hidden in the action bar | |
searchBox.setVisibility(View.INVISIBLE); | |
searchIcon.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
toggleSearch(false); | |
} | |
}); | |
searchBox.setOnClearListener(new OnClearListener() { | |
@Override | |
public void onClear() { | |
toggleSearch(true); | |
} | |
}); | |
searchBox.setOnItemClickListener(new AdapterView.OnItemClickListener() { | |
@Override | |
public void onItemClick(AdapterView<?> parent, View view, int position, long id) { | |
// handle clicks on search resaults here | |
} | |
}); | |
actionBar.setCustomView(v); | |
} | |
// this toggles between the visibility of the search icon and the search box | |
// to show search icon - reset = true | |
// to show search box - reset = false | |
protected void toggleSearch(boolean reset) { | |
ClearableAutoCompleteTextView searchBox = (ClearableAutoCompleteTextView) findViewById(R.id.search_box); | |
ImageView searchIcon = (ImageView) findViewById(R.id.search_icon); | |
if (reset) { | |
// hide search box and show search icon | |
searchBox.setText(""); | |
searchBox.setVisibility(View.GONE); | |
searchIcon.setVisibility(View.VISIBLE); | |
// hide the keyboard | |
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); | |
imm.hideSoftInputFromWindow(searchBox.getWindowToken(), 0); | |
} else { | |
// hide search icon and show search box | |
searchIcon.setVisibility(View.GONE); | |
searchBox.setVisibility(View.VISIBLE); | |
searchBox.requestFocus(); | |
// show the keyboard | |
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); | |
imm.showSoftInput(searchBox, InputMethodManager.SHOW_IMPLICIT); | |
} | |
} |
what type of control is R.id.search_box? and in which layout file it is residing?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
final ClearableAutoCompleteTextView searchBox = (ClearableAutoCompleteTextView) v.findViewById(R.id.search_box);
Getting parse error.....cannot parse ...