Reputation: 770
I have a problem getting the actionview from a menu item used in an actionbar. I'm using Actionbarsherlock for compatibility reasons.
I'm using this code in onCreateOptionMenu:
menu.add("Search")
.setIcon(R.drawable.ic_search)
.setActionView(R.layout.collapsible_edittext)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
The ActionView i set is just an EditText from XML which will "fill_parent" in terms of width. As i couldn't find a way to access this edittext to register an event handler i tried to inflate "R.layout.collapsible_edittext" in oncreateoptionsmenu (and later in onOptionsItemSelected) adding the result as acitonview. After i did this i could access the edittext but it didn't take the full width in my actionbar anymore after the icon was clicked.
Any hints how to access the ActionView properly?
Upvotes: 1
Views: 1781
Reputation: 13721
EditText layout
<?xml version="1.0" encoding="utf-8"?>
<EditText xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:hint="Search"/>
Code
private EditText search;
private TextWatcher filterTextWatcher = new TextWatcher() {
public void afterTextChanged(Editable s) {
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
//... your logic here
}
};
@Override
public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {
menu.add(0, 3, 3, R.string.ac_search ).setIcon(R.drawable.ic_action_search).setActionView(R.layout.action_search).setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(com.actionbarsherlock.view.MenuItem item) {
switch (item.getItemId()){
case 3:
search = (EditText) item.getActionView();
search.addTextChangedListener(filterTextWatcher);
break;
}
return true;
}
Upvotes: 2