How to make SearchView always expanded in android? - android

I have SearchView in the top of the layout (not in the action bar), is there anyway to force this View to be always expanded (opened)?
If not, i wish to place fancy image near it, is there anyway to make SearchView hide this image when expanded (clicked/expanded)?

You can use the property android:iconifiedByDefault="false" on XML or set programatically setIconifiedByDefault(false). Acording to the documentation this property set the SearchView expanded like you want.
Take a look at SearchView.setIconifiedByDefault(boolean iconified)

SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();
searchItem.expandActionView();
and in menu file, use
showAsAction="ifRoom|collapseActionView"
If any query, feel free to comment.

I am doing this into fragment ,
i done it by using
searchItem.expandActionView();
but when user press back button it collapse so in collapse method i used
getActivity().getSupportFragmentManager().popBackStack();
// when it collapsed i am going back
Below is my solution in detail:
search_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/search_contacts"
android:title="search"
android:icon="#drawable/search2"
app:showAsAction="ifRoom|collapseActionView"
app:actionViewClass="android.support.v7.widget.SearchView" >
</item>
</menu>
in fragment i used below code :
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.search_menu, menu);
// Define the listener
MenuItemCompat.OnActionExpandListener expandListener = new MenuItemCompat.OnActionExpandListener() {
#Override
public boolean onMenuItemActionCollapse(MenuItem item)
{
getActivity().getSupportFragmentManager().popBackStack();
// Do something when action item collapses
return true; // Return true to collapse action view
}
#Override
public boolean onMenuItemActionExpand(MenuItem item)
{
// Do something when expanded
return true; // Return true to expand action view
}
};
MenuItem searchItem = menu.findItem(R.id.search_contacts);
// Assign the listener to that action item
MenuItemCompat.setOnActionExpandListener(searchItem, expandListener);
// Any other things you have to do when creating the options menu…
SearchView searchView =
(SearchView) MenuItemCompat.getActionView(searchItem);
//searchView.setIconifiedByDefault(false);
searchItem.expandActionView(); // when fragment opens it expanded auto
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
#Override
public boolean onQueryTextChange(String newText)
{
seachViewFunction(newText); // in searchViewFunction(newText); i am doing my things
return false;
}
});
super.onCreateOptionsMenu(menu, inflater);
}

You can try to use
searchView.onActionViewExpanded();
and if you do not need keyboard opened
searchView.clearFocus();

Related

Make the SearchView do searches

I have a SearchView up and ready, here is the code I use in the java file of the page that has the SearchView:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.options_menu, menu);
// Associate searchable configuration with the SearchView
SearchManager searchManager =
(SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView =
(SearchView) menu.findItem(R.id.search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(
new ComponentName(getApplicationContext(), SearchableActivity.class)));
return true;
}
My goal is now to let this SearchView search through a site full of TextViews. Every TextView contains one word. Is it possible to make the SearchView find the TextView that contains the word i just typed in? If so, how? If not, how can I make it happen in another way?
Kind regards,
Julian
The SearchView doesn't do any search at all, it's a simple widget for inputting the keywords, show recently used keywords, etc. You should implement your "search model" to find the content, and display it somewhere you want.
I guess what you are trying to do is search through text inside TextViews defined inside your layout.
I am assuming here that the text source is a String Array/ArrayList (TextViewData) in a file "Data.java.
You can give this a try:
1) Make the activity/fragment implement SearchView.OnQueryTextListener
2) Create an empty ArrayList to store search entry results.
private ArrayList<Data> mSearchItems
3) Add this line to the code where you initialize your SearchView
searchView.setOnQueryTextListener(this);
4) Override the following methods:
i) Method called on submit:
#Override
public boolean onQueryTextSubmit(String query) {
//Call a user defined mathod to handle the search.
handleSearch(query, Data.TextViewData)
}
ii) Method called on changing the text in the search box
#Override
public boolean onQueryTextChange(String query) {
handleSearch(query, Data.TextViewData)
}
5)
private void handleSearch(String Query, ArrayList<Data> queryList){
mSearchItems.clear();
for(Data data : queryList){
if(data.textviewone.toLowerCase().contains(Query.toLowerCase()) ||
data.textviewtwo.toLowerCase().contains(Query.toLowerCase()))
mSearchItems.add(data);
//You can then swap the recycler view (if being used) or hide other text views and display only the one being searched for (in case of a match)
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(menu.findItem(R.id.menu_search));
SearchManager searchManager = (SearchManager) getSystemService(Activity.SEARCH_SERVICE);
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
// Setting up custom search button icon
int searchImgId = android.support.v7.appcompat.R.id.search_button; // I used the explicit layout ID of searchview's ImageView
ImageView v = (ImageView) searchView.findViewById(searchImgId);
v.setImageResource(R.drawable.ic_search_black);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
// Do operation
return true;
}
#Override
public boolean onQueryTextChange(String newText) {
return false;
}
});
return true;
}
menu_search.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:appcompat="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/menu_search"
style="#android:style/Widget.ActionButton"
android:icon="#drawable/ic_search_black"
android:title="#string/search"
appcompat:actionViewClass="android.support.v7.widget.SearchView"
appcompat:showAsAction="always" />
</menu>

Search button in toolbar for fragment is not displaying keyboard

I have multiple fragments in an activity where I want a search button in the toolbar. In one of my fragments this all successfully works but when I copied the exact same code into my other two fragments they aren't functioning properly.
When I click the search button on the working fragment, the keyboard pops up and I can start entering text. But on the other two fragments when I press on the search button icon, it slides to the left and then I need to press it again for it to work. Anyone know how I can fix this?
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.search, menu);
MenuItem item = menu.findItem(R.id.action_search);
SearchView sv = new SearchView(((Home) getActivity()).getSupportActionBar().getThemedContext());
MenuItemCompat.setShowAsAction(item, MenuItemCompat.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW | MenuItemCompat.SHOW_AS_ACTION_IF_ROOM);
MenuItemCompat.setActionView(item, sv);
sv.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
list.clear();
for (int i = 0; i < mainList.size(); i++) {
if (mainList.get(i).getName().toLowerCase().startsWith(newText.toLowerCase(), 0) || mainList.get(i).getAddress().toLowerCase().startsWith(newText.toLowerCase())) {
list.add(mainList.get(i));
}
}
adapter.notifyDataSetChanged();
return false;
}
});
super.onCreateOptionsMenu(menu, inflater);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId())
{
case R.id.action_search:
getActivity().onSearchRequested();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Try to define searchView in menu item.
<item android:id="#+id/action_search"
android:icon="#drawable/ic_search_white_24dp"
android:title="#string/search"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="collapseActionView|always" />
Hey i have same issue and i found solution for it. it may be because of you are giving a whole menu for search view. so when you click first time on search icon that full menu gets load and then you again click on search icon and then search view gets open.
Try below code
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater)
{
inflater.inflate(R.menu.searchview, menu);
MenuItem item = menu.findItem(R.id.menu_item_search);
SearchView sv = (SearchView) item.getActionView();
if (sv != null){
sv.setSubmitButtonEnabled(true);
sv.setOnQueryTextListener(this);
}
super.onCreateOptionsMenu(menu, inflater);
}
searchview.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
app:showAsAction="always|collapseActionView"
android:icon="#android:drawable/ic_menu_search"
android:id="#+id/menu_item_search"
android:orderInCategory="1"
app:actionViewClass="android.support.v7.widget.SearchView"
android:title="Search"
android:iconifiedByDefault="true"/>
</menu>
And make sure you import android.support.v7.widget.SearchView otherwise it will give error in casting.
Hope it will help.

android collapsable searchview with custom header

I need the suggestion for using the best way to show searchview with custom header.
My headerlayout should initially look like this
And after clicking the search button on the right of the header, it must replace header title and menu icon part with the search input field like this.
pressing back in this state, should direct to initial state.
How can I do this?
in Menu Resource directory
search_country_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/action_search"
android:icon="#drawable/abc_ic_search"
app:showAsAction="always|collapseActionView"
android:orderInCategory="0"
app:actionViewClass="android.support.v7.widget.SearchView"/>
</menu>
**In Activity's onCreateOptionmenu Method **
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.search_country_menu, menu);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
menu.findItem(R.id.action_search).expandActionView();
searchView.setQueryHint(getResources().getString(R.string.select_country));
searchView.setImeOptions(EditorInfo.IME_ACTION_DONE);
searchView.setOnQueryTextListener(new OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String arg0) {
searchView.clearFocus();
return true;
}
#Override
public boolean onQueryTextChange(String arg0) {
etSearch.setText(arg0);
return true;
}
});
return super.onCreateOptionsMenu(menu);
}
Do you want to customize the SearchViewor is a standard one fine for you?
If it is I suggest looking into the ActionBar with an ActionView
Android ActionBar documentation about adding an ActionView

Android ActionBar Search Field

I have a standard search EditText in my ActionBar, triggered by a touch on a little magnifying glass icon. I can cache the user's search string using the OnQueryTextListener. I want to put that string back into the EditText when the user touches the icon a second time.
I'm using ABS (soon to abandon), targeting 8-19.
How can I do this?
With a normal action bar (you'll have to find the different variation with ABS, not sure if my answer will apply completely with that library).
SearchView is a widget that can be inflated in the options menu, so what I do is inflate a menu using an XML containing an action view:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/search_user"
android:actionViewClass="android.widget.SearchView"
android:icon="#drawable/magnifing_glass"
android:showAsAction="ifRoom|collapseActionView"
android:title="Search Users"/>
</menu>
Next, when inflating the action view, set your listeners, and use a global variable to save the previous search.
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.search_menu, menu);
MenuItem search = menu.findItem(R.id.search_user);
//Keep a global variable of this so you can set it within the next listener
SearchView user_search = (SearchView) search.getActionView();
user_search.setOnQueryTextListener(new OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
global_variable = query;
return true;
}
#Override
public boolean onQueryTextChange(String text) {
return true;
}
});
Finally, in a second listener, set the text of the global variable to your previous query when the action view expands.
//This is set on the menu item
search.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) {
user_search.setQuery(global_variable, false);
return true; // Return true to expand action view
}
});

how to disable the collapsing of search view on back key pressed android

I have used Sherlock search view widget in action bar for search menu. The code snippet for initializing the search view
MenuItem item = menu.findItem(R.id.menu_search);
item.expandActionView();
mSearchView = (SearchView) item.getActionView();
mSearchView.setIconifiedByDefault(false);
mSearchView.setQuery(query, false);
mSearchView.clearFocus();
The above code shows expanded search view with default search query. The problem is when I press back button the search view again collapses before returning to previous activity in android. I don't want the search view to collapse on back key press. How to prevent search view from collapsing on back key press?
SearchView.setIconifiedByDefault(false) should be enough if you have set the attribute android:showAsAction="always" in your menu xml.
The following snippet works for me:
default_options.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
<item android:id="#+id/action_search"
android:title="#string/search"
android:icon="#drawable/topbar_busqueda"
androidshowAsAction="always"
android:actionViewClass="android.support.v7.widget.SearchView" />
</menu>
Activity
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.default_options menu);
MenuItem menuItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) menuItem.getActionView();
prepareSearchViewAdapter(searchView, menuItem);
super.onCreateOptionsMenu(menu, inflater);
}
private void prepareSearchViewAdapter(final SearchView searchView, MenuItem menuItem) {
searchView.setIconifiedByDefault(false);
}
Add a boolean value when search view is open like this
private Boolean isSearchPageIsOpen = true;
Then add the even to handle back button press
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_BACK:
if(isSearchPageIsOpen ){
do something here
}
return false;

Categories

Resources