I am trying to to implement SearchView with Sherlock Action Bar. I am not able to get the query to enter the onQueryTextChange method. Not sure whats going wrong.
public class MainActivity extends SherlockFragmentActivity implements
SearchView.OnQueryTextListener {
protected static CharSequence[] _categories = { "Amusement Park",
"Bird Sanctuary", "Wild Life", "River", "Hill Station", "Temple" };
protected static boolean[] _selections = new boolean[_categories.length];
private final String[] places = new String[] { "Mysore", "Bangalore",
"Mangalore", "Wayanad", "Bandipur National Park", "Chickmaglur",
"Bandipura", "Coorg", "Kodaikanal", "Hampi", "Ghati Subramanya",
"Mekedatu", "Muththathhi", "Shivasamudram", "Talakadu",
"Savana Durga" };
public SearchView mSearchView;
private TextView mStatusView;
private Menu mainMenu = null;
PlacesListAdapter adapter;
ListView listView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.i("Nomad", "onCreate");
List<Place> thePlaces = new ArrayList<Place>();
for (int i = 0; i < places.length; i++) {
Place pl = new Place("NO_ID", places[i], "NO_DISTANCE",
"NO_CATEGORYICON");
thePlaces.add(pl);
}
listView = (ListView) findViewById(R.id.place_list);
adapter = new PlacesListAdapter(MainActivity.this, R.layout.item_place,
thePlaces);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> a, View view, int position,
long id) {
Toast.makeText(MainActivity.this, "Test", Toast.LENGTH_SHORT);
startActivity(new Intent(MainActivity.this, PlaceActivity.class));
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
Log.i("Nomad", "onCreateOptionsMenu");
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.activity_main, menu);
mainMenu = menu;
MenuItem searchItem = menu.findItem(R.id.action_search);
// Search View
mSearchView = (SearchView) searchItem.getActionView();
setupSearchView(searchItem);
mSearchView.setOnSearchClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// hide action item
if (mainMenu != null) {
mainMenu.findItem(R.id.action_category).setVisible(false);
mainMenu.findItem(R.id.action_sort).setVisible(false);
}
}
});
mSearchView.setOnCloseListener(new SearchView.OnCloseListener() {
#Override
public boolean onClose() {
// re-show the action button
if (mainMenu != null) {
mainMenu.findItem(R.id.action_category).setVisible(true);
mainMenu.findItem(R.id.action_sort).setVisible(true);
}
return false;
}
});
Log.i("Nomad", "after setupSearchView()");
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_search:
Toast.makeText(this, "Searh", Toast.LENGTH_LONG).show();
Log.i("Nomad", "Click Search");
break;
case R.id.action_category:
showDialog();
break;
case R.id.action_sort_alpha_az:
Toast.makeText(this, "Alpha AZ", Toast.LENGTH_LONG).show();
break;
case R.id.action_sort_alpha_za:
Toast.makeText(this, "Alpha ZA", Toast.LENGTH_LONG).show();
break;
case R.id.action_sort_dist_nf:
Toast.makeText(this, "Dist NF", Toast.LENGTH_LONG).show();
break;
case R.id.action_sort_dist_fn:
Toast.makeText(this, "Dist FN", Toast.LENGTH_LONG).show();
break;
default:
// return super.onOptionsItemSelected(item);
break;
}
return true;
}
private void setupSearchView(MenuItem searchItem) {
mSearchView.setIconifiedByDefault(true);
mSearchView.setOnQueryTextListener(this);
mSearchView.setSubmitButtonEnabled(false);
searchItem.setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_ALWAYS);
// | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW
}
public boolean onQueryTextChange(String newText) {
Log.i("Nomad", "onQueryTextChange");
if (TextUtils.isEmpty(newText)) {
Log.i("Nomad", "onQueryTextChange Empty String");
listView.clearTextFilter();
} else {
Log.i("Nomad", "onQueryTextChange " + newText.toString());
adapter.getFilter().filter(newText.toString());
}
return true;
}
public boolean onQueryTextSubmit(String query) {
Log.i("Nomad", "onQueryTextSubmit");
return false;
}
public boolean onClose() {
Log.i("Nomad", "onClose");
return false;
}
protected boolean isAlwaysExpanded() {
return false;
}
}
Add this to your searchCommand :
searchCommand.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
Related
When I split the char how can use spannable or ClickableSpan to get values?
this is a code
StringBuilder sb = new StringBuilder();
String data = newFeedModel.getFeedTags();
String[] items = data.split("#");
for (String item : items)
{
if(item.trim().length() == 0){
sb.append("");
}else{
sb.append("#"+item+"\n");
}
}
holder.tags.setText(sb.toString());
I will split all "#" char, like facebook function.
#aaa#bbbccc#xcvsdfsefsefa#22
it will split like this
#aaa
#bbbccc
#xcvsdfsefsefa
#22
but how to know when I split the char then can got correct gata?
Update:
public void onBindViewHolder(ViewHolder holder, int position) {
if(!TextUtils.isEmpty(newFeedModel.getFeedTags())){
holder.tags.setVisibility(View.VISIBLE);
String data = newFeedModel.getFeedTags();
holder.bind(data);
}
}
in MyClickableSpan
class MyClickableSpan extends ClickableSpan {
String clicked;
Context context;
public MyClickableSpan(Context context, String string) {
super();
this.context = context;
clicked = string;
}
public void onClick(View tv) {
Toast.makeText(context.getApplicationContext(), ""+clicked, Toast.LENGTH_LONG).show();
}
#Override
public void updateDrawState(TextPaint ds) {
ds.setUnderlineText(false); // set to false to remove underline
}
}
how can send the data to live search when user click the tags.
this is my live code
public void fetchSearch(String key)
{
apiInterface = ApiClient.getApiClient().create(ApiInterface.class);
Call<List<SearchModel>> call = apiInterface.getSearch(key);
call.enqueue(new Callback<List<SearchModel>>() {
#Override
public void onResponse(Call<List<SearchModel>> call, retrofit2.Response<List<SearchModel>> response) {
searchModelList = response.body();
adapter = new NewSearchAdapter(getActivity(), searchModelList);
recyclerView.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
#Override
public void onFailure(Call<List<SearchModel>> call, Throwable t) {
}
});
}
#Override
public void onCreateOptionsMenu(#NonNull Menu menu, #NonNull MenuInflater inflater) {
inflater.inflate(R.menu.search, menu);
SearchManager searchManager = (SearchManager) getActivity().getSystemService(Context.SEARCH_SERVICE);
final SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getActivity().getComponentName()));
searchView.setIconifiedByDefault(false);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
fetchSearch(query);
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
fetchSearch(newText);
return false;
}
});
super.onCreateOptionsMenu(menu, inflater);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (item.getItemId()) {
case R.id.message:
Intent intent = new Intent(getContext(), NotificationMessageActivity.class);
startActivity(intent);
return true;
}
return super.onOptionsItemSelected(item);
}
I am trying to implement tab functionality in an Android browser. I tried to save the webpage in a cache, but I can't figure out how to save multiple of them and load them when the user switches tabs. How could I implement this?
I would like to save the current webpage in a cache when the user wants to add a new tab and open Main_activity, and then next time when the user switches tabs, the current webpage should get saved in a cache, and the intended webpage of the intended tab gets loaded in the webview. Now the issue is, I don't know how to handle multiple webpages in a cache, and select which one to load when.
My code consists of two activities, main_activity with some icons and webView_activity with a simple webview. I am using a custom dialog box with a ListView in it, so I will need to add new rows for new tabs and edit them when tabs are switched.
Here is my main_activity
public class MainActivity extends AppCompatActivity {
ImageView yt,google,gm,twitter;
SearchView g_search;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
g_search = (SearchView) findViewById(R.id.g_search);
g_search.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
Intent i = new Intent(MainActivity.this,webview1.class);
i.putExtra("query",query);
startActivity(i);
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
return false;
}
});
yt = (ImageView) findViewById(R.id.yt);
google = (ImageView) findViewById(R.id.google);
gm = (ImageView) findViewById(R.id.gm);
twitter = (ImageView) findViewById(R.id.twitter);
yt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(MainActivity.this,webview1.class);
i.putExtra("shortcut","https://www.youtube.com");
startActivity(i);
}
});
google.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(MainActivity.this,webview1.class);
i.putExtra("shortcut","https://www.google.com");
startActivity(i);
}
});
gm.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(MainActivity.this,webview1.class);
i.putExtra("shortcut","https://www.gmail.com");
startActivity(i);
}
});
twitter.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(MainActivity.this,webview1.class);
i.putExtra("shortcut","https://www.twitter.com");
startActivity(i);
}
});
}
#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.m1, menu);
// Associate searchable configuration with the SearchView
SearchManager searchManager =
(SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView =
(SearchView) menu.findItem(R.id.menu_search).getActionView();
searchView.setSearchableInfo(
searchManager.getSearchableInfo(getComponentName()));
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
Intent i = new Intent(MainActivity.this,webview1.class);
i.putExtra("query",query);
startActivity(i);
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
return false;
}
});
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId()==R.id.action_new_tab){
}
if(item.getItemId()==R.id.action_incognito_mode){
}
if(item.getItemId()==R.id.action_bookmarks){
}
if(item.getItemId()==R.id.action_history){
}
if(item.getItemId()==R.id.action_downloads){
}
if(item.getItemId()==R.id.action_settings){
Intent i = new Intent(MainActivity.this,SettingsActivity.class);
startActivity(i);
}
if(item.getItemId()==R.id.action_about){
Intent i = new Intent(MainActivity.this,about.class);
startActivity(i);
}
return super.onOptionsItemSelected(item);
}
boolean doubleBackToExitPressedOnce = false;
#Override
public void onBackPressed() {
if (doubleBackToExitPressedOnce) {
super.onBackPressed();
return;
}
this.doubleBackToExitPressedOnce = true;
Toast.makeText(this, "Please click BACK again to exit", Toast.LENGTH_SHORT).show();
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
doubleBackToExitPressedOnce=false;
}
}, 2000);
}
}
Here is my web_view activity
public class webview1 extends AppCompatActivity {
CustomWebView web1;
String search_q,shortcut;
FloatingActionButton fab;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_webview1);
web1 = (CustomWebView) findViewById(R.id.web1);
web1.setGestureDetector(new GestureDetector(new CustomGestureDetector()));
web1.setWebViewClient(new WebViewClient(){
#Override
public void onReceivedError(final WebView view, int errorCode, String description,
final String failingUrl) {
fragmentManager.beginTransaction()
.replace(R.id.container, new blankState())
.commit();
super.onReceivedError(view, errorCode, description, failingUrl);
}
});
registerForContextMenu(web1);
// web1.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
web1.getSettings().setJavaScriptEnabled(true);
web1.getSettings().setBuiltInZoomControls(true);
web1.getSettings().setDisplayZoomControls(false);
web1.getSettings().setLoadWithOverviewMode(true);
web1.getSettings().setUseWideViewPort(true);
search_q=getIntent().getStringExtra("query");
shortcut=getIntent().getStringExtra("shortcut");
bottomNavigationView = (BottomNavigationView) findViewById(R.id.navigation);
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setCustomView(R.layout.custom_toolbar);
ImageView action_back = (ImageView) findViewById(R.id.action_back);
action_back.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onBackPressed();
}
});
BottomNavigationViewHelper.disableShiftMode(bottomNavigationView);
bottomNavigationView.setVisibility(GONE);
fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// code for launching new tab here
}
});
web1.setWebChromeClient(new WebChromeClient(){
public void onProgressChanged(WebView view, int progress ){
frame_layout.setVisibility(View.VISIBLE);
progress_bar.setProgress(progress);
if(progress==100){
frame_layout.setVisibility(GONE);
srl.setRefreshing(false);
}
super.onProgressChanged(view, progress);
}
});
progress_bar.setProgress(0);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId()==R.id.action_url){
ShowUrlDialog();
return true;
}
if(item.getItemId()==R.id.action_home){
Intent i = new Intent(webview1.this,MainActivity.class);
startActivity(i);
}
if(item.getItemId()==R.id.action_new_tab){
// code to launch new tab here
}
if(item.getItemId()==R.id.action_incognito_mode){
}
if(item.getItemId()==R.id.action_bookmarks){
}
if(item.getItemId()==R.id.action_history){
}
if(item.getItemId()==R.id.action_downloads){
}
if(item.getItemId()==R.id.action_settings){
Intent i = new Intent(webview1.this,SettingsActivity.class);
startActivity(i);
}
if(item.getItemId()==R.id.action_about){
Intent i = new Intent(webview1.this,about.class);
startActivity(i);
}
return super.onOptionsItemSelected(item);
}
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
// Inflate menu to add items to action bar if it is present.
inflater.inflate(R.menu.m2, menu);
// Associate searchable configuration with the SearchView
return true;
}
#Override
public void onBackPressed() {
if (web1.canGoBack()) {
web1.goBack();
} else {
super.onBackPressed();
}
}
}
I'm trying to implement SearchView in standalone Toolbar in a fragment but unable to get any result.
I followed this answer SearchView at a secondary Toolbar but no success.
Here is my implementation
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_tab_fragment3, container, false);
initViews(rootView);
initBottomToolbar(rootView);
return rootView;
}
private void initBottomToolbar(View rootView) {
mToolbarBottom = (Toolbar) rootView.findViewById(R.id.toolbar_bottom);
// Set an OnMenuItemClickListener to handle menu item clicks
mToolbarBottom.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem menuItem) {
// Handle the menu item
Toast.makeText(getActivity(), "action_search clicked", Toast.LENGTH_SHORT).show();
SearchManager searchManager = (SearchManager) getActivity().getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = null;
if (menuItem != null) {
Log.d("Bottom Toolbar", "menuItem != null");
searchView = (SearchView) menuItem.getActionView();
}
if (searchView != null) {
Log.d("Bottom Toolbar", "searchView != null");
searchView.setSearchableInfo(searchManager.getSearchableInfo(getActivity().getComponentName()));
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
Toast.makeText(getActivity(), query, Toast.LENGTH_SHORT).show();
Log.e("Bottom Toolbar", "Submitted: "+query);
return false;
}
#Override
public boolean onQueryTextChange(String newQuery) {
Toast.makeText(getActivity(), newQuery, Toast.LENGTH_SHORT).show();
Log.d("Bottom Toolbar", "Changed: " + newQuery);
List<ContactsBean> filteredContactsBeanList = filter(mContactsBeanList, newQuery);
mContactsAdapter.animateTo(filteredContactsBeanList);
mContactsRecyclerView.scrollToPosition(0);
return true;
}
});
}
return true;
}
});
// Inflate a menu to be displayed in the toolbar
mToolbarBottom.inflateMenu(R.menu.menu_bottom_toolbar);
}
Even, if I click SearchView, it's neither showing Toast nor showing any message in Log.
I made it work by inflating first the Menu on my toolbarBottom before binding the listeners
toolbarBottom.inflateMenu(R.menu.menu_contacts_bottom);
if(toolbarBottom.getMenu().size() > 0){
SearchManager searchManager = (SearchManager) getActivity().getSystemService(
Context.SEARCH_SERVICE);
MenuItem searchMenuItem = toolbarBottom.getMenu().getItem(toolbarBottom.getMenu().size()-2);
mSearchView = (SearchView) searchMenuItem.getActionView();
if (mSearchView != null) {
mSearchView.setSearchableInfo(searchManager.getSearchableInfo(getActivity().getComponentName()));
}
mSearchView.setOnCloseListener(new OnCloseListener() {
#Override
public boolean onClose() {
Log.d(TAG, "onClose");
return false;
}
});
mSearchView.setOnQueryTextListener(new OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
Log.d(TAG, "query: " + newText);
return false;
}
});
}
the problem in search is start only of first word
example : search in "Real Madrid" , if search of "Madrid" not give result , but if search of "Real" give me result because the word "real" is first word
So what is the solution to this problem? Thank you :)
public void setAdapterToListview() {
objAdapter = new LatestGridAdapter(getActivity(), R.layout.latest_lsv_item,
arrayOfLatestVideo,columnWidth);
lsv_latest.setAdapter(objAdapter);
}
public void showToast(String msg) {
Toast.makeText(getActivity(), msg, Toast.LENGTH_LONG).show();
}
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.menu_search, menu);
final SearchView searchView = (SearchView) menu.findItem(R.id.search)
.getActionView();
final MenuItem searchMenuItem = menu.findItem(R.id.search);
searchView.setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
// TODO Auto-generated method stub
if(!hasFocus) {
searchMenuItem.collapseActionView();
searchView.setQuery("", false);
}
}
});
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextChange(String newText) {
textlength=newText.length();
arrayOfLatestVideo.clear();
for(int i=0;i< allArrayVideoName.length;i++)
{
if(textlength <= allArrayVideoName[i].length())
{
if(newText.toString().equalsIgnoreCase((String) allArrayVideoName[i].substring(0, textlength)))
{
ItemLatest objItem = new ItemLatest();
objItem.setId(Integer.parseInt(allArrayVideoId[i]));
objItem.setCategoryId(Integer.parseInt(allArrayVideoCatId[i]));
objItem.setCategoryName(allArrayVideoCatName[i]);
objItem.setDescription(allArrayVideoDesc[i]);
objItem.setVideoId(allArrayVideo[i]);
objItem.setVideoName(allArrayVideoName[i]);
objItem.setVideoUrl(allArrayVideourl[i]);
objItem.setImageUrl(allArrayImageUrl[i]);
arrayOfLatestVideo.add(objItem);
}
}
}
setAdapterToListview();
return false;
}
#Override
public boolean onQueryTextSubmit(String query) {
// Do something
return true;
}
});
}
Try this method for searching. If it returns anything other than -1 it means that it's found a match. I turn everything into lowercase so you don't have to worry about case sensitivity.
#Override
public boolean onQueryTextChange(String newText) {
textlength=newText.length();
arrayOfLatestVideo.clear();
for(int i=0;i< allArrayVideoName.length;i++)
{
if(textlength <= allArrayVideoName[i].length())
{
if(allArrayVideoName[i].toLowerCase().indexOf(newText.toLowerCase())!=-1)
{
ItemLatest objItem = new ItemLatest();
objItem.setId(Integer.parseInt(allArrayVideoId[i]));
objItem.setCategoryId(Integer.parseInt(allArrayVideoCatId[i]));
objItem.setCategoryName(allArrayVideoCatName[i]);
objItem.setDescription(allArrayVideoDesc[i]);
objItem.setVideoId(allArrayVideo[i]);
objItem.setVideoName(allArrayVideoName[i]);
objItem.setVideoUrl(allArrayVideourl[i]);
objItem.setImageUrl(allArrayImageUrl[i]);
arrayOfLatestVideo.add(objItem);
}
}
}
setAdapterToListview();
return false;
}
I am using Actionbar Sherlock and and i have three buttons
Search with a SearchView
Categories which opens a Dialog Fragment
Sort which opens a hiddent drop down menu
When i click the Search Button the SearchView text expands. When the Search view has expanded i want to hide all the other icons from the action bar and it should return when i exit the expanded searchView mode.
public class MainActivity extends SherlockFragmentActivity implements
SearchView.OnQueryTextListener {
protected static CharSequence[] _categories = { "Amusement Park",
"Bird Sanctuary", "Wild Life", "River", "Hill Station", "Temple" };
protected static boolean[] _selections = new boolean[_categories.length];
public SearchView mSearchView;
private TextView mStatusView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.activity_main, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
MenuItem categoryItem = menu.findItem(R.id.action_category);
MenuItem sortItem = menu.findItem(R.id.action_sort);
mSearchView = (SearchView) searchItem.getActionView();
setupSearchView(searchItem, categoryItem, sortItem);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
/*
* case R.id.action_go: Intent i = new Intent(MainActivity.this,
* PlaceActivity.class); startActivity(i); break;
*/
/*
* case R.id.action_search: Toast.makeText(this, "Searh",
* Toast.LENGTH_LONG).show(); break;
*/
case R.id.action_category:
showDialog();
break;
case R.id.action_sort_alpha_az:
Toast.makeText(this, "Alpha AZ", Toast.LENGTH_LONG).show();
break;
case R.id.action_sort_alpha_za:
Toast.makeText(this, "Alpha ZA", Toast.LENGTH_LONG).show();
break;
case R.id.action_sort_dist_nf:
Toast.makeText(this, "Dist NF", Toast.LENGTH_LONG).show();
break;
case R.id.action_sort_dist_fn:
Toast.makeText(this, "Dist FN", Toast.LENGTH_LONG).show();
break;
default:
// return super.onOptionsItemSelected(item);
break;
}
return true;
}
private void setupSearchView(MenuItem searchItem, MenuItem categoryItem,
MenuItem sortItem) {
mSearchView.setIconifiedByDefault(true);
searchItem.setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_ALWAYS
| MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
}
public boolean onQueryTextChange(String newText) {
Log.i("Nomad", "onQueryTextChange");
return false;
}
public boolean onQueryTextSubmit(String query) {
Log.i("Nomad", "onQueryTextSubmit");
return false;
}
public boolean onClose() {
Log.i("Nomad", "onClose");
return false;
}
protected boolean isAlwaysExpanded() {
return false;
}
}
I'm doing the same as follows:
private Menu mainMenu = null;
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.activity_main, menu);
mainMenu = menu;
MenuItem searchItem = menu.findItem(R.id.action_search);
MenuItem categoryItem = menu.findItem(R.id.action_category);
MenuItem sortItem = menu.findItem(R.id.action_sort);
mSearchView = (SearchView) searchItem.getActionView();
mSearchView.setOnSearchClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//hide action item
if (mainMenu != null)
mainMenu.findItem(R.id.quick_actions).setVisible(false);
}
});
mSearchView.setOnCloseListener(new SearchView.OnCloseListener() {
#Override
public boolean onClose() {
//re-show the action button
if (mainMenu != null)
mainMenu.findItem(R.id.quick_actions).setVisible(true);
return false;
}
});
//setupSearchView(searchItem, categoryItem, sortItem);
return true;
}
My solution is:
#Override
public void onCreateOptionsMenu(final Menu menu, MenuInflater inflater)
{
final MenuItem item = menu.add(0, MENU_SEARCH, 0, "Search");
final SearchView searchView = new SearchView(getActivity());
searchView.setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener()
{
#Override
public void onFocusChange(View view, boolean queryTextFocused)
{
if(!queryTextFocused)
{
item.collapseActionView();
}
int count = menu.size();
for (int i = 0; i < count; i ++)
{
MenuItem it = menu.getItem(i);
it.setVisible(item.equals(it)) || !queryTextFocused);
}
}
});
item.setActionView(searchView);
item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
}
Simply on the xml of menu, just give the attribute as app:showAsAction="collapseActionView|ifRoom" for seachview and to other menu items give it as app:showAsAction="ifRoom".This will give you the expected result.