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.
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
}
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With