How to remove screen overlay from searchview in Android? - android

As the title states, I would like to know how to remove the screen overlay from the SearchView. It's a bit ugly and annoying. Plus it gets in the way. I'm using android.support.v7.widget.SearchView.
Here's what it looks like:

I have done like below, and I get no any overlay.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
MenuItem searchItem = menu.findItem(R.id.search_city);
searchView = (SearchView) searchItem.getActionView();
searchView.setQueryHint("Search View Hint");
searchView.setOnQueryTextListener(new OnQueryTextListener() {
#Override
public boolean onQueryTextChange(String newText) {
//Log.e("onQueryTextChange", "called");
return false;
}
#Override
public boolean onQueryTextSubmit(String query) {
// Do your task here
return false;
}
});
return true;
}

Related

app focus close when open app

Blockquote
Im working with listview and Searchview.Below my code in Main activity.
enter code here
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_search, menu);
MenuItem item = menu.findItem(R.id.searbar);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(item);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
madapter.getFilter().filter(newText);
return false;
}
});
return super.onCreateOptionsMenu(menu);
// return true;
}
Blockquote
But app not open when run below code
enter code here
getMenuInflater().inflate(R.menu.menu_search, menu);
MenuItem item = menu.findItem(R.id.searbar);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(item);

Android SearchView not working when calling invalidateOptionsMenu()

I want to hide some menu items when the search view expands so this is the code I used:
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
menu.findItem(R.id.action_refresh).setVisible(!isSearchOpen)
}
#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 searchMenuItem = menu.findItem(R.id.action_search)
SearchView searchView = searchMenuItem.getMenu();
searchView.setOnQueryTextListener(this);
MenuItemCompat.setOnActionExpandListener(searchMenuItem, new MenuItemCompat.OnActionExpandListener() {
#Override
public boolean onMenuItemActionCollapse(MenuItem item) {
isSearchOpen = true;
invalidateOptionsMenu();
return true;
}
#Override
public boolean onMenuItemActionExpand(MenuItem item) {
isSearchOpen = false;
invalidateOptionsMenu();
return true;
}
});
}
Now this code works as expected except for one issue: when I click the search button, the SearchView does not appear. I cannot figure out what is the problem. I searched for similar issues on stack overflow but I didn't found any answer the fixed the issue, so any help will be welcomed
You can try this way to hide and show other options menu:-
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.search_menu, menu);
final MenuItem delMenu = menu.findItem(R.id.action_delete);
final MenuItem editMenu = menu.findItem(R.id.action_edit);
MenuItem searchMenu = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) searchMenu.getActionView();
searchView.setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View view, boolean b) {
if(b) {
delMenu.setVisible(false);
editMenu.setVisible(false);
}
}
});
searchView.setOnCloseListener(new SearchView.OnCloseListener() {
#Override
public boolean onClose() {
delMenu.setVisible(true);
editMenu.setVisible(true);
return false;
}
});
return true;
}

android Menu search button

I am trying to put a search button on action bar. And i have a list on the screen, on searching any text list will be refreshed and display that text data on the same screen.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
final MenuItem item = menu.findItem(R.id.search);
return true;
}
On click search button expends and soft keyboard show search button to search.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.search:
// startSearchActivity();
MenuItemCompat.expandActionView(item);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(item);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String text) {
Toast.makeText(getApplicationContext(), "dddddd " + text, Toast.LENGTH_LONG).show();
return true;
}
#Override
public boolean onQueryTextChange(String text) {
Toast.makeText(getApplicationContext(), "sssssss " + text, Toast.LENGTH_LONG).show();
return true;
}
});
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
but search button of softkeyboard is not showing any action. Please tell me whats wrong ..
implement setOnQueryTextListener in onCreateOptionsMenu instead of onOptionsItemSelected
#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);
MenuItemCompat.expandActionView(searchItem);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String text) {
Toast.makeText(getApplicationContext(), "dddddd " + text, Toast.LENGTH_LONG).show();
return true;
}
#Override
public boolean onQueryTextChange(String text) {
Toast.makeText(getApplicationContext(), "sssssss " + text, Toast.LENGTH_LONG).show();
return true;
}
});
return true;
}

android searchView in action bar how to use it?

I have a SearchView in a ActionBar. Now I want to use it but I don't know how to use it. In the custom SearchView I use it like this:
SearchView sear = (SearchView) findViewById(R.id.searchView);
sear.setOnQueryTextListener(new SearchView.OnQueryTextListener()
{
#Override
public boolean onQueryTextSubmit (String query){
return false;
}
#Override
public boolean onQueryTextChange (String newText){
listDataAdapter.getFilter().filter(newText);
return true;
}
}
);
Now I have SearchView in an action bar which I show through code:
public class DataListActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.data_list_layout);
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
// Inflate menu to add items to action bar if it is present.
inflater.inflate(R.menu.menu_main, menu);
// Associate searchable configuration with the SearchView
SearchManager searchManager =
(SearchManager) getSystemService(Context.SEARCH_SERVICE);
android.support.v7.widget.SearchView searchView =
(android.support.v7.widget.SearchView) menu.findItem(R.id.menu_search).getActionView();
searchView.setSearchableInfo(
searchManager.getSearchableInfo(getComponentName()));
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
Intent intent = new Intent(getApplicationContext(),MainMenu.class);
startActivity(intent);
return true;
case R.id.action_Exit:
openExit();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void openExit() {
}
How can I use SearchView ? I don't know how to use SearchView in action bar.
Set the onQueryTextListener(..) similar to what you have mentioned in your question on the searchView.

How to overlay activity content when using SearchView?

After I tap the Search icon, I want the SearchView widget to expand, and cover up the content below it. Inbox masks the current content by overlaying an opaque gray color above it. How do I do the same thing? I am using the SearchView widget.
I achieved it by simply switching to another fragment when the searchview is expanded, and switch back when it is collapsed.. The code looks something like this:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
//Setup the search widget
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
MenuItem menuItem = menu.findItem(R.id.action_search);
SearchView mSearchView = (SearchView) menuItem.getActionView();
//Not sure if you need this line for the searchView to be expandable
mSearchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
mSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String s) {
//Update contents of the fragment
}
#Override
public boolean onQueryTextChange(String s) {
//Update contents of the fragment
}
});
MenuItemCompat.setOnActionExpandListener(menuItem,
new MenuItemCompat.OnActionExpandListener() {
#Override
public boolean onMenuItemActionExpand(MenuItem item) {
//Switch to search fragment
}
#Override
public boolean onMenuItemActionCollapse(MenuItem item) {
//Switch back to previous fragment
}
})
;
return super.onCreateOptionsMenu(menu);
}

Categories

Resources