I am trying to implement Action Bar using ActionBar Sherlock. I have three Action Button one of which is a Search Button. On Clicking of the Search Button the Search input field should be displayed which i have already implemented. But i want it to take the full width of the Action Bar. Any idea how i can achieve the same.
Asked
Active
Viewed 1.6k times
18

Harsha M V
- 54,075
- 125
- 354
- 529

suresh cheemalamudi
- 6,190
- 11
- 49
- 67
-
1Have you tried looking at the ActionBarSherlock samples? https://github.com/JakeWharton/ActionBarSherlock/tree/master/samples/demos/src/com/actionbarsherlock/sample/demos – Alex Fu Nov 27 '12 at 18:35
-
@ Alex Fu:Ya i am using API demos as reference to code. but it dint help me to acheive that particular requirement. – suresh cheemalamudi Nov 28 '12 at 06:09
-
Did you try to use SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW for menu items? – jumper0k Nov 28 '12 at 11:06
-
No. I have no idea how to add(SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW). could u please suggest.? – suresh cheemalamudi Nov 28 '12 at 11:28
-
@suresh -- https://github.com/JakeWharton/ActionBarSherlock/blob/master/samples/demos/src/com/actionbarsherlock/sample/demos/CollapsibleActionItem.java No sure how you could of missed this if you looked through the demo source code. – Alex Fu Nov 28 '12 at 12:24
-
1@Alex_Fu unfortunately your web links are broken. – intrepidis Jun 10 '13 at 08:19
1 Answers
23
first make a editTextLayout
layout_search.xml
<?xml version="1.0" encoding="utf-8"?>
<EditText xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/search_edit_text"
android:cursorVisible="true"
android:hint="@string/search_friend_hint"
android:imeOptions="actionDone"
android:inputType="text"
android:textColor="@android:color/black"
android:textCursorDrawable="@android:color/black" />
In you menu xml add android:actionLayout
and android:showAsAction="always|collapseActionView"
for Search option.
For other option make android:showAsAction="ifRoom"
menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:icon="@drawable/ic_action_sort"
android:orderInCategory="1"
android:showAsAction="ifRoom"
android:title="@string/menu_sort"/>
<item
android:id="@+id/menu_search"
android:actionLayout="@layout/layout_search"
android:icon="@drawable/search"
android:orderInCategory="0"
android:showAsAction="always|collapseActionView"
android:title="@string/search"/>
</menu>
in your activity or fragment override onCreateOptionsMenu like this fragment.java
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.menu, menu);
final EditText editText = (EditText) menu.findItem(
R.id.menu_search).getActionView();
editText.addTextChangedListener(textWatcher);
MenuItem menuItem = menu.findItem(R.id.menu_search);
menuItem.setOnActionExpandListener(new OnActionExpandListener() {
@Override
public boolean onMenuItemActionCollapse(MenuItem item) {
// Do something when collapsed
return true; // Return true to collapse action view
}
@Override
public boolean onMenuItemActionExpand(MenuItem item) {
editText.clearFocus();
return true; // Return true to expand action view
}
});
}
and add textWatcherListener
private TextWatcher textWatcher = new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
if (null != mAdapter) {
mAdapter.getFilter().filter(s);
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
};

JafarKhQ
- 8,676
- 3
- 35
- 45
-
-
@cringe its the adapter name(mAdapter: a ListView adapter), i have change it – JafarKhQ Oct 13 '13 at 14:03
-
@Mehdiway you can see its a 'background', A PNG Image. you can remove it if you want the default editText bg. – JafarKhQ May 13 '14 at 13:10
-
@XiJiaopin if you use custom adapter (BaseAdapter), you have to implement the filter, check ArrayAdapter code for the Filter – JafarKhQ Jul 23 '14 at 22:19