I'm getting mad with using SearchView in a fragment and the back button is always shown whatever i touched the search bar or not and i do the search but i have a problem that back button do nothing but clear the text in search i would like to use it to switch also between fragments. I've tried many solutions in stack overflow questions but seems not solving my issue.
the code after adding toolbar in the fragment:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.search_menu_fragment, menu);
MenuItem searchItem = menu.findItem(R.id.action_search_locations);
SearchView searchView = new SearchView(((AppCompatActivity)getActivity()).getSupportActionBar().getThemedContext());
MenuItemCompat.setShowAsAction(searchItem, searchItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW ); //searchItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW |
MenuItemCompat.setActionView(searchItem, searchView);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
// filter recycler view when query submitted
locationStatesPreviewAdapter.getFilter().filter(query);
return false;
}
#Override
public boolean onQueryTextChange(String query) {
// filter recycler view when text is changed
Log.e("QueryChange",query);
locationStatesPreviewAdapter.getFilter().filter(query);
return false;
}
});
}
the xml file has only the search item in the menu :
<?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_locations"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:title="#string/app_name"
android:icon="#android:drawable/ic_menu_search"
app:showAsAction="always|collapseActionView"
app:actionViewClass="android.support.v7.widget.SearchView" />
</menu>
Thanks.
That was a problem of mis-understanding, as i thought it's the back button of the SearchView in Fragment and afterwards it was the toolbar Navigation button and after adding this lines to make it responsive and all things done ! thanks for help all.
((AppCompatActivity)getActivity()).setSupportActionBar(toolbar);
((AppCompatActivity)getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
((AppCompatActivity)getActivity()).getSupportActionBar().setTitle("TOOLBAR");
toolbar.setNavigationOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view) {
Toast.makeText(getActivity(), "Back clicked!", Toast.LENGTH_SHORT).show();
}
});
Related
I have the problem, I use icon search on Toolbar, I want to click this icon search, it will move to new activity and expand editview search in here, but my code can not achive
My class (First Activity):
#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_icon_toolbar, menu);
// Retrieve the SearchView and plug it into SearchManager
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
if(item.getItemId()==R.id.action_search){
Intent i = new Intent(this,SearchCarActivity.class);
startActivity(i);
return true;
}
return super.onOptionsItemSelected(item);
}
my layout_activity:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<!-- NavigationDrawer Menu -->
<item android:id="#+id/action_search"
android:title="Search"
app:showAsAction="always"
android:icon="#drawable/ic_action_name_search"
app:actionViewClass="android.support.v7.widget.SearchView"
/>
</menu>
My Search activity (Second activity):
public class SearchCarActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_result);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.search_car_menu, menu);
SearchView searchView =
(SearchView) menu.findItem(R.id.search).getActionView();
searchView.setIconified(false);
return super.onCreateOptionsMenu(menu);
}
}
My search_car_menu.xml:
<?xml version="1.0" encoding="utf-8"?>
<item
android:id="#+id/search"
android:icon="#android:drawable/ic_menu_search"
android:title="Search"
app:showAsAction="always"
app:actionViewClass="android.support.v7.widget.SearchView"/>
It does't move to second activity, it seem still achive in first activity
How to I can achive move to second activity when clicked icon search on Toolbar?
Thanks
I created a test project an added your code
from what you said you want to click on the search icon on the first activity and then open second activity and do your search there
removing this line from your layout_activity did the trick :
app:actionViewClass="android.support.v7.widget.SearchView"
when you added that line your item would behave as a SearchView and not even respond to
public boolean onOptionsItemSelected(MenuItem item)
One Solution is to do it in same activity.
Set listener for input query text
searchView.setOnQueryTextListener(MyActivity.this);
and implement MyActivity with this interface SearchView.OnQueryTextListener
This interface provides 2 callback methods:
#Override
public boolean onQueryTextSubmit(String query) {
//get this query and search on server
}
#Override
public boolean onQueryTextChange(String newText) {
return false;
}
Hope this helps you.
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.
I have an activity with action bar and one icon on this ActionBar. When user press this icon I need to hide this icon and show EditText with cross to clear text. When user will done with it (choose item in ListView or press back button) I will need to show icon again.
Here is two related question:
1. How to add EditText on ActionBar for ListView filtering?
2. How to change ActionBar between EditText and icon?
Before click:
After click on 'plus' icon:
You need to add a SearchView widget to your application.
The official documentation is here:
http://developer.android.com/training/search/setup.html
There are also detailed instructions to add it.
searchmenu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/action_search"
android:title="search"
android:icon="#android:drawable/ic_menu_search"
android:showAsAction="always"
android:actionViewClass="android.widget.SearchView" />
</menu>
mainactivity.java
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.searchmenu, menu);
mSearchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
setupSearchView();
return super.onCreateOptionsMenu(menu);
}
private void setupSearchView() {
mSearchView.setIconifiedByDefault(true);
mSearchView.setOnQueryTextListener(this);
mSearchView.setQueryHint("Search Here");
}
#Override
public boolean onQueryTextChange(String newText) {
//implement the filterng techniques
return false;
}
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
How to add EditText on ActionBar for ListView filtering?
Please refer to this answer by Commonsware . More clearly his sample project will do the trick.
How to change ActionBar between EditText and icon?
Simply override onOptionItemSelected and try to set the visibility of that menu item.
MenuItem item = menu.findItem(R.id.my_item);
item.setVisible(false);
Alternatively , If you want full control in your hands try to customize your action bar
I've started messing around with fragments, as my app on tablets could be using space more efficiently.
Now, in my old app, I had a SearchView in the actionbar menu, that would show on ifRoom. I'm using the same code, but now the SearchView item is always null. I'm not looking for changing the menu items depending on active fragments or whatever, I just need the SearchView to function from the FragmentActivity.
FragmentActivity:
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.main, menu);
MenuItem searchItem = menu.findItem(R.id.svPet);
this.svPet = (SearchView)MenuItemCompat.getActionView(searchItem);
if(this.svPet != null)
{
this.svPet.setInputType(InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
this.svPet.setOnQueryTextListener(this);
}
return true;
}
main.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/svPet"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="ifRoom"
android:title="#string/search"
android:titleCondensed="#string/search">
</item>
...Some more items that are never shown as action and work correctly
</menu>
So, whenever the menu is being created, this.svPet stays null, the searchview is not displayed in the bar (even when there's more than enough room), and when I click on the item from the menu, my application crashes, saying there's a nullpointer on
this.svPet.setIconified(false);
in
#Override
public boolean onOptionsItemSelected(MenuItem item)
Any ideas on what might be wrong? I'm probably overlooking something, but I just don't see what's wrong at the moment. Thanks in advance :)
some ideas
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
//SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
SearchView searchView = (SearchView) searchItem.getActionView();
if (searchView != null) {
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String s) {
// do something with s, the entered string
query = s;
Toast.makeText(getApplicationContext(), "String entered is " + s,Toast.LENGTH_SHORT).show();
return true;
}
#Override
public boolean onQueryTextChange(String s) {
return false;
}
});
}
return super.onCreateOptionsMenu(menu);
}
menu.xml
<item android:id="#+id/action_search"
android:orderInCategory="5"
android:title="Search"
android:icon="#drawable/ic_action_search"
android:showAsAction="ifRoom|collapseActionView"
android:actionViewClass="android.widget.SearchView" />
</menu>
So... after some digging I found that when using MenuItemCompat in combination with FragmentActivity, getActionView(searchItem) returns null. I think this is a bug in the support library, so I'll have to submit a bugreport for that. (Done, https://code.google.com/p/android/issues/detail?id=76141&thanks=76141&ts=1410649918)
My workaround:
Instead of
this.svPet = (SearchView)MenuItemCompat.getActionView(searchItem);
I used
this.svPet = new SearchView(this);
MenuItemCompat.setShowAsAction(searchItem, MenuItemCompat.SHOW_AS_ACTION_IF_ROOM);
MenuItemCompat.setActionView(searchItem, this.svPet);
--Edit--
After some more looking around, I found that you can just use ActionBarActivity instead of FragmentActivity, as ActionBarActivity extends FragmentActivity. When using that, you can use
this.svPet = (SearchView)MenuItemCompat.getActionView(searchItem);
as usual.
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();