how add search in actionbar sherlock - android

I am using the actionbar sherlock. Now I want to add a search option to actionbar and intent Edittext search to other activity.
can anyone help me?

Do something like this
#Override
public boolean onCreateOptionsMenu(Menu menu) {
//Used to put dark icons on light action bar
//Create the search view
final SearchView searchView = new SearchView(getSupportActionBar().getThemedContext());
searchView.setQueryHint("Search");
menu.add(Menu.NONE,Menu.NONE,1,"Search")
.setIcon(R.drawable.abs__ic_search)
.setActionView(searchView)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
searchView.setOnQueryTextListener(new OnQueryTextListener() {
#Override
public boolean onQueryTextChange(String newText) {
if (newText.length() > 0) {
// Search
} else {
// Do something when there's no input
}
return false;
}
#Override
public boolean onQueryTextSubmit(String query) {
InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(searchView.getWindowToken(), 0);
Toast.makeText(getBaseContext(), "dummy Search", Toast.LENGTH_SHORT).show();
setSupportProgressBarIndeterminateVisibility(true);
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
setSupportProgressBarIndeterminateVisibility(false);
}
}, 2000);
return false; }
});
return true;
}

Related

Collapse SearchView (in ActionBar) on Back button press

I have implemented SearchView in ActionBar, But the problem with it is, whenever am typing something and press submit, the SearchView doesn't collapse back into that Search icon, it stays expanded, neither when I press Back button, SearchView doesn't collapse back.
My implementation is like this:
menu_search.xml
<item
android:id="#+id/app_bar_search"
android:icon="#drawable/ic_search_white_24dp"
android:title="Search"
app:actionViewClass="android.widget.SearchView"
app:showAsAction="always" />
MainActivity.java
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_search, menu);
item = menu.findItem(R.id.app_bar_search);
searchView = (SearchView) item.getActionView();
searchView.setMaxWidth(Integer.MAX_VALUE);
int searchBarId = searchView.getContext().getResources().getIdentifier("android:id/search_bar", null, null);
searchBar = searchView.findViewById(searchBarId);
searchBar.setLayoutTransition(new LayoutTransition());
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
item.collapseActionView();
searchView.setQuery("", false);
searchView.clearFocus();
searchBar.clearFocus();
.
.
.
.
.
return true;
}
#Override
public boolean onQueryTextChange(String newText) {
return false;
}
});
return super.onCreateOptionsMenu(menu);
}
Things I have tried:
#Override
public void onBackPressed() {
super.onBackPressed();
if (item.isActionViewExpanded()) {
item.collapseActionView();
} else {
super.onBackPressed();
}
}
and
#Override
public void onBackPressed() {
super.onBackPressed();
if (searchView != null) {
searchView.onActionViewCollapsed();
searchView.setQuery("", false);
searchView.clearFocus();
searchBar.clearFocus();
hideSoftKeyboard(MainActivity.this);
} else {
super.onBackPressed();
}
}
public static void hideSoftKeyboard(Activity activity) {
InputMethodManager inputMethodManager =
(InputMethodManager) activity.getSystemService(
Activity.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(
activity.getCurrentFocus().getWindowToken(), 0);
}
SearchView expands from left to right to type but doesn't collapse back form right to left.
Please advice.
Try below code and let me know if it works.
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
searchView.setSearchableInfo(searchManager.getSearchableInfo(new ComponentName(this, ResultActivity.class)));
searchView.setImeOptions(EditorInfo.IME_ACTION_SEARCH);
searchView.setOnSuggestionListener(new SearchView.OnSuggestionListener() {
#Override
public boolean onSuggestionSelect(int position) {
return false;
}
#Override
public boolean onSuggestionClick(int position) {
searchView.clearFocus();
item.collapseActionView();
return false;
}
});
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
searchView.clearFocus();
item.collapseActionView();
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
return false;
}
});
I was going through the second page of Google search and found this.
It worked for me.
searchView.setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
searchView.setIconified(true);
}
}
});

focus on searchview in menu when a button is clicked

There is a searchview in menu and i have a button. When the button is clicked i need the searchview to open for search which means focus should be on searchview for enter search texts.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_fooddoof, menu);
searchView = (SearchView) MenuItemCompat.getActionView(menu.findItem(R.id.search));
TextView searchText = (TextView)
searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
Typeface myCustomFont = Typeface.createFromAsset(getAssets(), "fonts/ProximaNovaRgRegular.ttf");
searchText.setTypeface(myCustomFont);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
Intent intent = new Intent(descStore.this, listdisplay.class);
intent.putExtra("deskey", query);
startActivity(intent);
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
//android.widget.Filter filter = descAdapter.getFilter();
//filter.filter(newText);
return true;
}
});
searchView.setSubmitButtonEnabled(true);
return true;
}
When click a button:
#Override
public void onClick(View v) {
if (v == mViews.description1) {
searchview.seticonified(false);
}
}
I've tried seticonified option but it is not working . Please help.
You could request the focus of the searchview as below
#Override
public void onClick(View v) {
switch (view.getId()) {
case R.id.your_button_id:
searchview.seticonified(false);
searchview.requestFocus(); //request focus of the view
break;
}
}

onQueryTextChange triggered after app resume

I am stumbed with animated searchview onQueryTextListener. When activity and fragment created first it works nice. Then I press home button, open other apps, do some work there to wipe the data of searchview activity and then return to the app. And when activity and fragment resume onQueryTextChange method is triggered by it's own. I tried this issue
Fragment replacement triggers onQueryTextChange on searchview
but it did not help, helps only when searchview SHOW_AS_ACTION_NEVER, but in this case I can not see searchview. How to prevent self-triggering of OnQueryTextListener?
Snippet from fragment
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
searchView = new SearchView(getSherlockActivity().getSupportActionBar()
.getThemedContext());
searchView.setOnQueryTextListener(new OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
if (newText.length() > 0) {
fpAdapter.getFilter().filter(newText);
} else {
loadData();
}
return false;
}
});
TextView searchText = (TextView) searchView
.findViewById(R.id.abs__search_src_text);
searchText.setTextColor(Color.WHITE);
searchText.setCursorVisible(false);
ImageView searchButton = (ImageView) searchView
.findViewById(R.id.abs__search_button);
searchButton.setImageResource(R.drawable.search_menu_button);
LinearLayout searchEditFrame = (LinearLayout) searchView
.findViewById(R.id.abs__search_edit_frame);
searchEditFrame.setBackgroundResource(android.R.color.transparent);
View searchPlate = searchView.findViewById(R.id.abs__search_plate);
searchPlate.setBackgroundColor(Color.argb(0, 0, 0, 0));
menu.add("Search")
.setActionView(searchView)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
final MenuItem searchMenuItem = menu.getItem(0);
final Animation in = AnimationUtils.loadAnimation(getSherlockActivity()
.getApplicationContext(), R.anim.search_in);
final Animation out = AnimationUtils.loadAnimation(
getSherlockActivity().getApplicationContext(),
R.anim.search_out);
searchView.setQueryHint(getResources().getText(
R.string.search_messages_hint));
searchView.setIconifiedByDefault(true);
searchView
.setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View view,
boolean queryTextFocused) {
if (!queryTextFocused) {
// searchView.startAnimation(out);
searchMenuItem.collapseActionView();
} else {
searchView.startAnimation(in);
}
}
});
super.onCreateOptionsMenu(menu, inflater);
}
Update: This appears only in HTC sensation XL with Android 4.0.3, on 4.2 I don't see this problem.
Found the only one solution - set listener in onResume:
#Override
public void onResume() {
super.onResume();
searchView.setOnQueryTextListener(new OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
if (newText.length() > 0) {
fpAdapter.getFilter().filter(newText);
} else {
loadData();
}
return false;
}
}); }
use this code
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String s) {
return false;
}
#Override
public boolean onQueryTextChange(final String s) {
if(searchView.getWidth()>0)
{
// your code here
}
return false;
}
});
You can set to use the SearchView only when it's in focus. I had a similar problem where the search was performing in my fragment every time when users resumed it. I solve it with the following method:
-Add a boolean to see when SearchView is in focus:
//by default the SearchView isn't in focus so set it to false.
private boolean shouldSearch = false;
searchView.setOnQueryTextFocusChangeListener((view, hasFocus) -> {
if (hasFocus) {
shouldSearch = true;
} else {
shouldSearch = false;
}
});
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
if (shouldSearch) {
//do your search here
}
return true;
}
});
This is a known issue. You can fix by checking to see if the search is "iconified" i.e only showing the magnifying glass icon or if it is expanded ( which means the user is interacting with the search view)
searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
override fun onQueryTextSubmit(query: String?): Boolean {
//your code here
return true
}
override fun onQueryTextChange(newText: String?): Boolean {
//this will catch firing event
if (searchView.isIconified) {
return true
}
//your code here
return true
}
})

Acton bar action menu - SearchView

I have a search view as an action item. Every thing is working fine except that I want to intercept the search executed event. At that time a new activity is started with search results. When that happens I want to collaps the search view in the previous activity. So when user comes back out of the search result activity, the action menu should be collapsed.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
//Used to put dark icons on light action bar
//Create the search view
final SearchView searchView = new SearchView(getSupportActionBar().getThemedContext());
searchView.setQueryHint("Search");
menu.add(Menu.NONE,Menu.NONE,1,"Search")
.setIcon(R.drawable.abs__ic_search)
.setActionView(searchView)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
searchView.setOnQueryTextListener(new OnQueryTextListener() {
#Override
public boolean onQueryTextChange(String newText) {
if (newText.length() > 0) {
// Search
} else {
// Do something when there's no input
}
return false;
}
#Override
public boolean onQueryTextSubmit(String query) {
InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(searchView.getWindowToken(), 0);
Toast.makeText(getBaseContext(), "dummy Search", Toast.LENGTH_SHORT).show();
setSupportProgressBarIndeterminateVisibility(true);
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
setSupportProgressBarIndeterminateVisibility(false);
}
}, 2000);
return false; }
});
return true;
}
I think you can collapse SearchView in onQueryTextSubmit method, see above code

Collapse searchview after submit

I am using searchview in my application ( without action bar). How can I collapse searchview after query text submit?
I have these listeners ;
#Override
public boolean onQueryTextSubmit(String query) {
InputMethodManager imm = (InputMethodManager)thisFr.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(globalSearch.getWindowToken(), 0);
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
// TODO Auto-generated method stub
return false;
}
I don't use ActionBar so I don't have a function like collapseActionView().
you need to call setIconified(true) twice to actually collapse your search view, with first call text is cleared with second call keyboard and search view get closed.
You can do it this way in your activity, tested with actionbarsherlock (it even hides the keyboard, make sure to return false in onQueryTextSubmit):
private MenuItem searchMenuItem;
public MenuItem getSearchMenuItem() {
return searchMenuItem;
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// ...
searchMenuItem = menu.findItem(R.id.menu_search);
// ...
searchView.setOnQueryTextListener(new OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
MenuItem searchMenuItem = getSearchMenuItem();
if (searchMenuItem != null) {
searchMenuItem.collapseActionView();
}
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
// ...
return true;
}
});
// ...
return super.onCreateOptionsMenu(menu);
}
If you are using the SearchView in the OptionsMenu, you ca call invalidateOptionsMenu()
//close suggestion list on query text submit
searchView.setIconified(true);
final MenuItem searchterm = menu.findItem(R.id.search);
SearchView searchView = null;
searchView = (SearchView) searchterm.getActionView();
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
searchterm.collapseActionView();
}});

Categories

Resources