I am trying to implement an Action bar search on a listview which is nested inside a Fragment.
For now I am just trying to print the search text onto console, but its not working.
Following is my code:
main.xml:
<item
android:id="#+id/search"
android:actionViewClass="android.widget.SearchView"
android:icon="#drawable/ic_search"
android:showAsAction="collapseActionView|ifRoom"
android:title="search"/>
Class Def:
public class FragmentTab1 extends Fragment{
this is my onCreateOptionsMenu:
public boolean onCreateOptionsMenu(Menu menu) {
System.out.println("menu inflater");
MenuInflater inflater = getActivity().getMenuInflater();
inflater.inflate(R.menu.main, menu);
SearchView searchView = (SearchView)menu.findItem(R.id.search).getActionView();
SearchView.OnQueryTextListener textChangeListener = new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextChange(String newText) {
System.out.println("Text"+newText);
return true;
}
#Override
public boolean onQueryTextSubmit(String query) {
System.out.println("on query submit: "+query);
return true;
}
};
searchView.setOnQueryTextListener(textChangeListener);
return true;
}
However when I type in the search box, nothing gets written onto the console, any hints what I am missing?
Try This Code.
#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(getComponentName()));
return true;
}
The problem here is with the method Declaration, in a fragment we have to use:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
With this everything is working as should be, however I have to look for a way that my MainActivity menu gets invalidated as soon as the Fragment is up.
Related
I am using Search view in the toolbar. I am facing issue with the close icon.
It is working as excepted on mobile but not working on Android tablets. On the tablet, it is showing in the middle of the action bar instead of right side.
As shown in below images:
On Mobile (Correct):
On Tablet (Incorrect):
My XML Code:
<?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"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="collapseActionView|ifRoom"
android:icon="#mipmap/ic_search_white_24dp"
android:title="Search" />
</menu>
My JAVA Code:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
menu.clear();
inflater.inflate(R.menu.menu_search_view_explore_ideas, menu);
SearchManager searchManager =
(SearchManager) getActivity().getSystemService(Context.SEARCH_SERVICE);
searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
searchView.setQueryHint("Search...");
((EditText) searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text)).setTextColor(Color.WHITE);
((EditText) searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text)).setHintTextColor(getResources().getColor(R.color.notify_user_color));
if (searchManager != null) {
searchView.setSearchableInfo(
searchManager.getSearchableInfo(getActivity().getComponentName()));
}
AutoCompleteTextView searchTextView = searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
try {
Field mCursorDrawableRes = TextView.class.getDeclaredField("mCursorDrawableRes");
mCursorDrawableRes.setAccessible(true);
mCursorDrawableRes.set(searchTextView, R.drawable.cursor_for_search_view); //This sets the cursor resource ID to 0 or #null which will make it visible on white background
} catch (Exception e) {
}
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
mAdapter.getFilter().filter(query);
return true;
}
#Override
public boolean onQueryTextChange(String newText) {
mAdapter.getFilter().filter(newText);
return true;
}
});
}
Let me know if I am missing something.
you try like this in your onCreateOptionsMenu
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_search_view_explore_ideas, menu);
SearchView searchView = (SearchView)menu.findItem(R.id.action_search).getActionView();
searchView.setMaxWidth(Integer.MAX_VALUE);
In your onCreateOptionsMenu() try this for searchView after inflating menu,
searchView.setMaxWidth(Integer.MAX_VALUE);
In your onCreateOptionsMenu()
try following code for searchView after inflating menu,
SearchView searchView= (SearchView)menu.findItem(R.id.action_search).getActionView();
searchView.setMaxWidth(Integer.MAX_VALUE);
Try this on onCreateOptionsMenu
searchView.setMaxWidth(Integer.MAX_VALUE);
In your OnCreateView
make following change
final SearchView searchView = (SearchView) menuItem.getActionView();
searchView.setMaxWidth(Integer.MAX_VALUE);
Here is what I did so far -
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.main, menu);
super.onCreateOptionsMenu(menu, inflater);
getActivity().setTitle("DASHBOARD");
SearchManager searchManager = (SearchManager)getContext(). getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getActivity().getComponentName()));
searchView.setIconifiedByDefault(false);
SearchView.OnQueryTextListener textChangeListener = new SearchView.OnQueryTextListener()
{
#Override
public boolean onQueryTextChange(String newText)
{
// this is your adapter that will be filtadapter.getFilter().filter(newText);
System.out.println("on text chnge text: "+newText);
return true;
}
#Override
public boolean onQueryTextSubmit(String query)
{
// this is your adapter that will be filtered
adapter.getFilter().filter(query);
System.out.println("on query submit: "+query);
return true;
}
};
searchView.setOnQueryTextListener(textChangeListener);
}
Here code Search view in action bar, it only filter the one name other does not work . The code adapter class code is below . Actionbar doing in fragment
adapter = new FavouritesAdapter(getContext(),distanceArr,contactName,status,leadDetailsId,addressLine1,addressLine2,city,state,zipCode);
list.setAdapter(adapter);
list.setTextFilterEnabled(true);
I am working on the following screen:
1 Screenshot
Here on the toolbar ,i am having a search icon.To add this search icon ,i have used the following code:
menu_friend_list_activity.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/search"
android:orderInCategory="100"
android:title="#string/action_search"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="always" />
</menu>
Code inside Activity
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.menu_friend_list_activity, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_search:
Toast.makeText(getApplicationContext(), "Search button clicked", Toast.LENGTH_SHORT).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
On clicking search icon i want to display the SearchView in toolbar(if i am not wrong here).In short i want to implement the following screen:
Please tell me how can i implement this.Search will be different for different fragments.
try this
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.search_actions, menu);
MenuItem searchViewItem = menu.findItem(R.id.action_search);
// Get the SearchView and set the searchable configuration
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) searchViewItem.getActionView();
searchView.setQueryHint("Search for Product,Brands...");
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false);// Do not iconify the widget; expand it by defaul
SearchView.OnQueryTextListener queryTextListener = new SearchView.OnQueryTextListener() {
public boolean onQueryTextChange(String newText) {
// This is your adapter that will be filtered
Toast.makeText(getApplicationContext(),"textChanged :"+newText,Toast.LENGTH_LONG).show();
return true;
}
public boolean onQueryTextSubmit(String query) {
// **Here you can get the value "query" which is entered in the search box.**
Toast.makeText(getApplicationContext(),"searchvalue :"+query,Toast.LENGTH_LONG).show();
return true;
}
};
searchView.setOnQueryTextListener(queryTextListener);
return true;
}
Your not inflating the menu correctly, Here is how your supposed to inflate it to get the screen you want
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.yourmenyxml, menu);
item = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(item);
I had similar issue. and I solved as following:
MenuItem searchItem = menu.findItem(R.id.ot_action_search);
final SearchView searchView = (SearchView) searchItem.getActionView();
searchItem.expandActionView();
searchView.setIconified(false);
searchView.setQuery(searchQuery, false);
NB: the expandActionView() should be invoked on searchItem menu item not on the searchView. that was my mistake.
i have searchview in the action bar it is not working i try to do this like as
#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()));
searchView.setIconifiedByDefault(true);
return true;
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener(){
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
return false;
}
});
}
but it give me error that android.support.v7.widget.SearchViewOnQueryTextListener in searchview can not be apply
#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()));
searchView.setIconifiedByDefault(true);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener(){
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
return false;
}
});
return true;
}
Return should be at the end,
last three braces where not in correct order.
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.