In the menu, I have search view. when I clicked search in menu search view should expand with customized search button.like below image currently it is opening like normal search
I want to change the default submit button with custom button
SearchManager searchManager =
(SearchManager) getSystemService(Context.SEARCH_SERVICE);
searchView =
(SearchView) menu.findItem(R.id.action_search).getActionView();
EditText searchEditText = (EditText) searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
searchView.setSubmitButtonEnabled(true);
searchView.setSearchableInfo(
searchManager.getSearchableInfo(getComponentName()));
Any help much appreciated.
ImageView b = (ImageView) searchView.findViewById(android.support.v7.appcompat.R.id.search_go_btn);
b.setImageResource(R.drawable.your_button_drawable);
try this one to change default search submit button
Related
I have the search view at the top of my layout I want when I( clicked the button the cursor should show in the search bar.
here is the image enter image description here
Create an ID for the SearchView and use this code to create actions within it.
#Override
public boolean onCreateOptionsMenu(Menu menu){
getMenuInflater().inflate(R.menu.beneficiary_search, menu);
MenuItem search = menu.findItem(R.id.searchicon);
SearchView searchView = (SearchView) search.getActionView();
searchView.requestFocus()
searchView.setOnQueryTextListener(this);
return true;
}
I am having a weird problem. In my MainActivity toolbar I have an action search and this is how I use it:
#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_main, menu);
MenuItem searchItem = menu.findItem(R.id.search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
searchView.setOnQueryTextListener(this);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
searchView.setSearchableInfo(searchManager.getSearchableInfo(
new ComponentName(this, SearchableActivity.class)));
searchView.setIconified(false);
return true;
}
but the problem here is that when my app starts the keyboard pops up before I even click the search button in the toolbar. I tried to force the keyboard to hide in the manifest I added this to my MainActivity:
android:windowSoftInputMode="stateAlwaysHidden"
I even tried this:
android:windowSoftInputMode="stateHidden"
but they keyboard still pops up. I don't have any EditText or anything I only have an image and a FAB in my MainActivity.
1) android:windowSoftInputMode="adjustPan|stateHidden"
2) getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
3) mSearchView.clearFocus();
I want to set an expanding animation to a searchview and I try to apply the method that I found:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
MenuItem item = menu.findItem(R.id.action_search);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) item.getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(true);
final int searchBarId = searchView.getContext().getResources().getIdentifier("android:id/search_bar", null, null);
LinearLayout searchBar = (LinearLayout) searchView.findViewById(searchBarId);
searchBar.setLayoutTransition(new LayoutTransition());
return true;
}
However, the variable searchBar is always null and it always crashes at the line
searchBar.setLayoutTransition(new LayoutTransition());
I don't know what is causing the error. Can someboby help me out? Thanks!
I had the same problem. It turns out that you get null in your searchBar if you use android.support.v7.widget.SearchView
I changed it to android.widget.SearchView (recommended if supporting api > 11). Now I get the needed linear layout and animation works fine.
Edit
If you need a support version, you have to use proper id: LinearLayout searchBar = (LinearLayout) mFilterInput.findViewById(android.support.v7.appcompat.R.id.search_bar);
I want to show Back button but distorted ToolBar is coming on left as in the following image.
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_main, menu);
MenuItem item = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(item);
TextView tv = (TextView) searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
tv.setTextColor(getResources().getColor(android.R.color.white));
tv.setHintTextColor(getResources().getColor(android.R.color.white));
tv.setHint(getString(R.string.action_search));
ImageView imgCloseIcon = (ImageView) searchView.findViewById(android.support.v7.appcompat.R.id.search_close_btn);
imgCloseIcon.setImageResource(R.drawable.cross_btn);
searchView.setOnQueryTextListener(this);
}
How to show Back button and hide distorted ToolBar ??
I have used the below code and its working fine for me,
SearchView searchView = (SearchView) MenuItemCompat.getActionView(menu.findItem(R.id.action_search));
SearchManager searchManager = (SearchManager) getSystemService(SEARCH_SERVICE);
searchView.setSearchableInfo(searchManager
.getSearchableInfo(getComponentName()));
searchView.setOnQueryTextListener(queryTextListener);
I use searchview in my Android app and I would like to add a button that user presses to start the search. Based on what I read on the Internet, I can use setSubmitButtonEnabled to invoke a submit button instead of putting a button in the layout file. Here's my code:
public void setSubmitButtonEnabled (boolean enabled) {
}
I put my setSubmitButtonEnabled in the menu inflater as below:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.mylist, menu);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false);
setSubmitButtonEnabled(true);
return true;
}
Apparently I am not doing it right because when I launch my app, I don't see any submit button on the screen. What's missing or what's wrong in my code? Is the submit button supposed to appear on the keypad or on the screen? Thank you.
You need to call
searchView.setSubmitButtonEnabled(true)
Why would you create your own version with no body and expect it to do anything?
For this use
searchView=findViewById(R.id.search_box);
searchView.setSubmitButtonEnabled(true);
For submit button icon use
app:goIcon="#drawable/ic_arrow_forward_black_24dp"
For SearchView default expanded
app:iconifiedByDefault="false"