Show searchview in actionbar only in parent fragment - android

I am wondering how to show searchview in actionbar only in parent fragment. For now, when I submit a search from searchview in actionbar and go to next fragment, I still can see the searchview in my child fragment. Any suggestions to fix that? Thanks.
For now, in my parent class
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
// Inflate the options menu from XML
inflater.inflate(R.menu.homesearchbar, menu);
super.onCreateOptionsMenu(menu,inflater);
// Get the SearchView and set the searchable configuration
SearchManager searchManager = (SearchManager) getActivity().getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
searchView.setMaxWidth(Integer.MAX_VALUE);
searchView.setIconifiedByDefault(false); // Do not iconify the widget; expand it by default
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
Bundle bundle = new Bundle();
bundle.putString("HomeToSearch_keyword", query);
SearchVC searchVC = new SearchVC();
searchVC.setArguments(bundle);
getFragmentManager().beginTransaction().add(R.id.HomeVC_frameChildFragment, searchVC)
.addToBackStack(null)
.commit();
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
return false;
}
});
}
homesearchbar.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:title=""
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="always"/>
</menu>

To hide searchView inside child fragment override inside it onPrepareOptionsMenu method. Then inside that method you can do something like this: menu.findItem(R.id.search).setVisible(false); or eventually when needed you can change it to true. And also don't forget to override onCreate method where you will put setHasOptionsMenu(true);

Related

Two searchviews showed up when switching tabs

Hi i am trying to add "search" functionality to my multiple tabs fragment. it is working fine with one tab, i am able to search and filter data no problem. So i use the same code for my other tabs, however, now the action bar show two search icon instead of one. I tried many different ways by hiding the searchview during detach/ attach action when switching tab, but still two search icons still pop up. One of the search icon will work for my current tab, and the other does not.
here is my code:
upcoming_event_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_upcoming_event_search"
android:title="search event"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="always"/>
</menu>
past_event_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_past_event_search"
android:title="search event"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="always"/>
</menu>
fragment 1:
public class EventTabUpcoming extends Fragment implements SearchView.OnQueryTextListener {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.event_tab_upcoming, container, false);
RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.listUpcomingEvent);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
recyclerView.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL));
recyclerView.setAdapter(sectionAdapter);
setHasOptionsMenu(true);
return view;
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.upcoming_event_menu, menu);
MenuItem item = menu.findItem(R.id.action_upcoming_event_search);
searchView = (SearchView) MenuItemCompat.getActionView(item);
searchView.setOnQueryTextListener(this);
}
#Override
public boolean onQueryTextChange(String searchText) {
Log.d(logTag, "EventTabUpcoming -> onQueryTextChange w/ searchText: " + searchText);
for (Section section : sectionAdapter.getCopyOfSectionsMap().values()) {
if (section instanceof FilterableEventSection) {
((FilterableEventSection) section).filter(searchText);
}
}
sectionAdapter.notifyDataSetChanged();
return true;
}
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
#Override
public void onDetach() {
super.onDetach();
Log.d(logTag, "onDetach is called");
if (searchView != null) {
searchView.clearFocus();
searchView.setVisibility(View.GONE);
}
}
#Override
public void onAttach(Context context) {
super.onAttach(context);
Log.d(logTag, "onAttach is called");
if (searchView != null) {
searchView.setVisibility(View.VISIBLE);
}
}
Fragment 2 is a cloned from Fragment 1 except the view is now:
final View view = inflater.inflate(R.layout.event_tab_past, container, false);
and menu is
inflater.inflate(R.menu.past_event_menu, menu);
Please let me know how should i correct this problem.
Here is my solution:
Instead of inflate the menu in both tab's activities, i inflate the menu in the viewpager activity only, and in each tab fragment's onCreateOptionMenu, i get the searchview and setQueryTextListener.
ViewPager activity:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
/**
* Inflate menu with searchview item
*/
inflater.inflate(R.menu.event_menu, menu);
}
Tab 1
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
# get the searchview and set listener
MenuItem item = menu.findItem(R.id.action_event_search);
searchView = (SearchView) MenuItemCompat.getActionView(item);
searchView.setOnQueryTextListener(this);
}
Tab 2
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
# get the searchview and set listener
MenuItem item = menu.findItem(R.id.action_event_search);
searchView = (SearchView) MenuItemCompat.getActionView(item);
searchView.setOnQueryTextListener(this);
}

Search view toolbar close icon

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);

How to use SearchView in the toolbar on clicking search icon

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.

Adding SearchView in Fragment

Trying to include a searchview in actionbar. For this, I have done the following:
Created MenuSearch.xml in the menu folder as given below:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/action_search"
android:actionViewClass="Android.Support.V7.Widget.SearchView"
android:icon="#android:drawable/ic_menu_search"
android:showAsAction="always"
android:title="Search"/>
</menu>
Included the method OnCreateOptionsMenu in the fragment:
public override void OnCreateOptionsMenu(IMenu menu, MenuInflater inflater)
{
inflater.Inflate(Resource.Menu.MenuSearch, menu);
base.OnCreateOptionsMenu(menu, inflater);
var searchManager = (SearchManager) this.Activity.GetSystemService(Context.SearchService);
searchView = (SearchView) (menu.FindItem(Resource.Id.action_search).ActionView);
searchView.SetSearchableInfo(searchManager.GetSearchableInfo(Activity.ComponentName));
}
And I have set HasOptionsMenu = true; in oncreate of the fragment.
With the code above, I get null pointer in the line searchView.SetSearchableInfo... . Not sure why this happens, though everything seem to be correct and in place.
Based on some suggestions for resolving similar issue, I have also tried out the below option for searchview.
var item = menu.FindItem (Resource.Id.action_search);
var searchItem = MenuItemCompat.GetActionView(item);
searchView = searchItem.JavaCast<Android.Support.V7.Widget.SearchView>();
searchView.SetSearchableInfo(searchManager.GetSearchableInfo(Activity.ComponentName));
But again, I get searchView as null.
Now that I have tried the possible options, I am clueless as to why searchview becomes null.
Any help is much appreciated.
Change your MenuSearch.xml to
<?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:title="#string/app_name"
android:icon="#drawable/ic_action_search"
app:showAsAction="collapseActionView|ifRoom"
/>
</menu>
And in your fragment, add searchView.setOnQueryTextListener
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
menu.clear();
inflater.inflate(R.menu.search_option_menu, menu);
MenuItem item = menu.findItem(R.id.action_search);
SearchView searchView = new SearchView(((MainActivity) mContext).getSupportActionBar().getThemedContext());
// MenuItemCompat.setShowAsAction(item, //MenuItemCompat.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW | //MenuItemCompat.SHOW_AS_ACTION_IF_ROOM);
// MenuItemCompat.setActionView(item, searchView);
// These lines are deprecated in API 26 use instead
item.setShowAsAction(MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW | MenuItem.SHOW_AS_ACTION_IF_ROOM);
item.setActionView(searchView);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
return false;
}
});
searchView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
}
);
}
Answer updated for 2018. Sorry for Kotlin, if you're using Java.
Your xml menu resource (search_menu.xml for example):
<?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:title="#string/app_name"
android:icon="#drawable/ic_search"
app:showAsAction="collapseActionView|ifRoom"
/>
</menu>
Build-up code:
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater?) {
super.onCreateOptionsMenu(menu, inflater)
menu.clear()
inflater?.inflate(R.menu.search_menu, menu)
val searchView = SearchView((context as MainActivity).supportActionBar?.themedContext ?: context)
menu.findItem(R.id.action_search).apply {
setShowAsAction(MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW or MenuItem.SHOW_AS_ACTION_IF_ROOM)
actionView = searchView
}
searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
override fun onQueryTextSubmit(query: String): Boolean {
return false
}
override fun onQueryTextChange(newText: String): Boolean {
return false
}
})
searchView.setOnClickListener {view -> }
}
You'll probably also need to put this line in onCreate() (worked for my fragment) to get this all to appear:
setHasOptionsMenu(true)
For late 2018 and for apis >= 26 compatible (deprecation warnings) and a properly formatted answer:
The 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:title="Search_change_me_with_proper_id"
android:icon="#drawable/ic_action_search"
app:showAsAction="collapseActionView|ifRoom"/>
</menu>
Java code inside the ActivityFragment:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
setHasOptionsMenu(true); // Add this!
return inflater.inflate(R.layout.fragment_add_app, container, false);
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
menu.clear();
inflater.inflate(R.menu.menu_add_app, menu);
MenuItem item = menu.findItem(R.id.action_search_menu_add_app);
item.setShowAsAction(MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW | MenuItem.SHOW_AS_ACTION_IF_ROOM);
SearchView searchView = (SearchView) item.getActionView();
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
// Here is where we are going to implement the filter logic
return true;
}
});
}
OR as a second option you let the ActivityFragment implement SearchView.OnQueryTextListener and then use the menu search:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
setHasOptionsMenu(true); // Add this! (as above)
return inflater.inflate(R.layout.fragment_add_app, container, false);
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
menu.clear();
inflater.inflate(R.menu.menu_add_app, menu);
MenuItem item = menu.findItem(R.id.action_search_menu_add_app);
item.setShowAsAction(MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW | MenuItem.SHOW_AS_ACTION_IF_ROOM);
SearchView searchView = (SearchView) item.getActionView();
searchView.setOnQueryTextListener(this); // Fragment implements SearchView.OnQueryTextListener
}
#Override
public boolean onQueryTextChange(String query) {
// Here is where we are going to implement the filter logic
return false;
}
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
i solved this issue i wants to share my answer for searchView NullPointerException
we need to remove below method from Main Activity
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
return true;
and use inside fragment in OnActivityCreated setHasOptionsMenu(true)
and Override methods
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu, menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// handle item selection
switch (item.getItemId()) {
case R.id.action_search:
// onCall(); //your logic
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Your search_options_menu.xml should look like this:
<?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:title="#string/app_name"
android:icon="#drawable/ic_action_search"
app:showAsAction="collapseActionView|ifRoom"
/>
</menu>
Override onCreateOptionMenu method in your fragment setHasOptionsMenu(true); inside onCreate() on your fragment.
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
menu.clear();
inflater.inflate(R.menu.search_option_menu, menu);
MenuItem item = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(menuItem);
MenuItemCompat.setShowAsAction(item, MenuItemCompat.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW | MenuItemCompat.SHOW_AS_ACTION_IF_ROOM);
MenuItemCompat.setActionView(item, searchView);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
//Do search code here
return true;
}
});
}
search_view.setOnQueryTextListener(object: SearchView.OnQueryTextListener {
override fun onQueryTextSubmit(query: String?): Boolean {
return false
}
override fun onQueryTextChange(query: String?): Boolean {
adapter.filter.filter(query)
return true
}
})
And don't add the intent filter for your activity in Manifest file.
In my fragment's onCreateOptionsMenu the code
SearchManager searchManager =
(SearchManager)getActivity().getSystemService(Context.SEARCH_SERVICE);
SearchableInfo searchableInfo =
searchManager.getSearchableInfo(getActivity().getComponentName());
searchItem = menu.findItem(R.id.menu_search);
SearchView searchView = (SearchView) searchItem.getActionView();
searchView.setSearchableInfo( searchableInfo);
produced following error message:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.SearchView.setSearchableInfo (android.app.SearchableInfo)' on a null object reference
After i changed in menu.xml android:actionViewClass to app:actionViewClass the problem did not occur again.
menu.xml :
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="...">
<item
android:id="#+id/menu_search"
android:title="#string/menu_search"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="ifRoom">

How can I hide the back button in Searchview

Does anyone know how to hide the back button in AppCompat v21 searchview? (outlined by green line)
I've searched a lot but couldn't find anything useful.
menu_main.xml:
<item android:id="#+id/search"
android:title="#string/search_title"
app:showAsAction="always|collapseActionView"
android:icon="#drawable/abc_ic_search_api_mtrl_alpha"
android:orderInCategory="300"
app:actionViewClass="android.support.v7.widget.SearchView" />
<item android:id="#+id/action_home"
android:title="Home"
android:icon="#drawable/v_home"
app:showAsAction="always"
android:orderInCategory="180"/>
<item android:id="#+id/action_favorites"
android:title="Favorites"
android:icon="#drawable/v_favorites"
app:showAsAction="always" />
MainActivity:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_main, menu);
firstMenu = menu;
searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
MenuItem searchItem = menu.findItem(R.id.search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setSubmitButtonEnabled(true);
searchView.setActivated(true);
searchView.setOnSearchClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
menuItemsVisibility(false);
}
});
searchView.setOnCloseListener(new SearchView.OnCloseListener() {
#Override
public boolean onClose() {
menuItemsVisibility(true);
return false;
}
});
return true;
}
#Override
public void onBackPressed() {
menuItemsVisibility(true);
super.onBackPressed();
}
// setting visibility of menu items on search
private void menuItemsVisibility(boolean visibility) {
MenuItem homeItem = firstMenu.findItem(R.id.action_home);
MenuItem favoriteItem = firstMenu.findItem(R.id.action_favorites);
MenuItem otItem = firstMenu.findItem(R.id.action_ot);
MenuItem ntItem = firstMenu.findItem(R.id.action_nt);
homeItem.setVisible(visibility);
favoriteItem.setVisible(visibility);
otItem.setVisible(visibility);
ntItem.setVisible(visibility);
}
Note: the behavior showAsAction:Always and using methods menuItemsVisibility() to adjust the visibility of toolbar items is intentional.
Another Note: MainActivity extends ActionBarActivity and it also implements implements ObservableScrollViewCallbacks from ObservableScrollView Library.
changed app:showAsAction="always|collapseActionView" to app:showAsAction="always"
Use the method:
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
in order to remove the home button from the action bar.
It's not perfectly safe method because back button (navigation up) doesn't have id. But if you are using AppCompat with Toolbar, you can use this code to find it. It should be the first in the layout.
int count = this.getToolbar().getChildCount();
for(int i = 0; i < count; ++i) {
View v = this.getToolbar().getChildAt(i);
if(v instanceof ImageButton) {
return (ImageButton)v;
}
}
Call this method inside onPrepareOptionsMenu if you want to change drawable of that button.

Categories

Resources