Faced with impossibility to change toolbar button icon from AlertDialog. ("Cannot resolve symbol").
Tried to declare toolbar button in other places in a code and got no result. Please help to solve it. How should I declare this button to avoid this error?
//Menu
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
MenuItem mChangeCur = menu.findItem(R.id.change_cur);
mChangeCur.setIcon(R.drawable.usd);
MenuItem mSearch = menu.findItem(R.id.action_search);
SearchView mSearchView = (SearchView) mSearch.getActionView();
mSearchView.setQueryHint("Search");
mSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
adapter.getFilter().filter(newText);
return true;
}
});
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
Intent intent;
switch (item.getItemId()) {
case R.id.change_cur:
// setup the alert builder
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Choose currency");
// add a list
String[] currencies = {"USD", "RUB", "AUD", "BRL", "CAD"};
builder.setItems(currencies, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case 0:
cur = "USD";
new Refresh().execute();
mChangeCur.setIcon(R.drawable.rub); //Here is mistake
break;
case 1:
cur = "RUB";
new Refresh().execute();
mChangeCur.setIcon(R.drawable.rub); //Here is mistake
break;
case 2:
case 3:
case 4:
default:
break;
}
}
});
Problem was in declaring and initialization mChangeCur. I had to declare mChangeCur on class level and initialize in public boolean onCreateOptionsMenu(Menu menu)
Related
I have a navigation drawer and I would like to add a view (Button, etc) to the menu. I don't want to use XML, therefore I am not using an inflator. I tried the setActionView method based on these examples at https://www.codota.com/android/methods/android.view.MenuItem/setActionView but I am getting an empty menu. Is what I am trying to do is possible? Here is my relevant part of code:
public boolean onCreateOptionsMenu(Menu menu) {
MenuItem item = menu.add("search");
SearchView sv = new SearchView(this);
item.setActionView(sv);
return true;
}
private static final int MENU_ITEM_ITEM1 = 1;
...
#Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(Menu.NONE, MENU_ITEM_ITEM1, Menu.NONE, "Item name");
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case MENU_ITEM_ITEM1:
//your action
return true;
default:
return false;
}
}
There is a searchview in menu and i have a button. When the button is clicked i need the searchview to open for search which means focus should be on searchview for enter search texts.
#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_fooddoof, menu);
searchView = (SearchView) MenuItemCompat.getActionView(menu.findItem(R.id.search));
TextView searchText = (TextView)
searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
Typeface myCustomFont = Typeface.createFromAsset(getAssets(), "fonts/ProximaNovaRgRegular.ttf");
searchText.setTypeface(myCustomFont);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
Intent intent = new Intent(descStore.this, listdisplay.class);
intent.putExtra("deskey", query);
startActivity(intent);
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
//android.widget.Filter filter = descAdapter.getFilter();
//filter.filter(newText);
return true;
}
});
searchView.setSubmitButtonEnabled(true);
return true;
}
When click a button:
#Override
public void onClick(View v) {
if (v == mViews.description1) {
searchview.seticonified(false);
}
}
I've tried seticonified option but it is not working . Please help.
You could request the focus of the searchview as below
#Override
public void onClick(View v) {
switch (view.getId()) {
case R.id.your_button_id:
searchview.seticonified(false);
searchview.requestFocus(); //request focus of the view
break;
}
}
I am using SearchView and it is working fine but only setOnCloseListener is not working; Here is my code
import com.actionbarsherlock.widget.SearchView.OnCloseListener;
and
searchView.setOnCloseListener(new OnCloseListener() {
#Override
public boolean onClose() {
Toast.makeText(context, "close", Toast.LENGTH_SHORT).show();
return false;
}
});
**EDIT****
#Override
public boolean onCreateOptionsMenu(Menu menu) {
//Used to put dark icons on light action bar
//Create the search view
final SearchView searchView = new SearchView(getSupportActionBar().getThemedContext());
searchView.setQueryHint("Search");
searchView.setIconifiedByDefault(true);
//search button
menu.add(Menu.NONE,Menu.NONE,1,"Search a word")
.setIcon(R.drawable.abs__ic_search_api_holo_light)
.setActionView(searchView)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
MenuItem sView = menu.findItem(1);
sView.setOnActionExpandListener(this);
return true;
}
#Override
public boolean onMenuItemActionCollapse(MenuItem item) {
adopter.getFilter().filter(null);
Toast.makeText(getApplicationContext(), "collapse", Toast.LENGTH_LONG).show();
return true; // Return true to collapse action view
}
#Override
public boolean onMenuItemActionExpand(MenuItem item) {
Toast.makeText(getApplicationContext(), "Expand", Toast.LENGTH_LONG).show();
return true; // Return true to expand action view
}
Solved it by myself. Just leave setOnCloseListener it will not work, and put following code in onCreateOptionsMenu
// searchView.setOnCloseListener(new OnCloseListener() {
// #Override
// public boolean onClose() {
// adapter.getFilter().filter("");
// Toast.makeText(getBaseContext(), "on close", Toast.LENGTH_SHORT).show();
// return false;
// }
// });
MenuItem menuItem = menu.findItem(ID_OF_SEARCHVIEW);
menuItem.setOnActionExpandListener(new OnActionExpandListener() {
#Override
public boolean onMenuItemActionExpand(MenuItem item) {
adapter.getFilter().filter("");
return true;
}
#Override
public boolean onMenuItemActionCollapse(MenuItem item) {
adapter.getFilter().filter("");
return true;
}
});
I have encountered the same problem with onCloseListener not invoking for the SearchView.
Understand from the bug issue raised in 25758, and some postings I have read, to invoke onCloseListener, you need to set:
searchView.setIconifiedByDefault(true);
But for my case I wanted to have the search view opened & not iconified all the time. I manage to resolve this by adding one more line below:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.search_bar, menu);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
searchView = (SearchView) menu.findItem(R.id.search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setOnQueryTextListener(queryTextListener);
searchView.setIconifiedByDefault(true);
searchView.setIconified(false);
return true;
}
The searchView.setIconified(false) will cause the searchView to open up, despite setting the default to iconified to true in the previous line. In this way, I managed to have both a SearchView
that opens up all the time & having it invoke the onCloseListener.
I'm using this code. And it works perfectly
#Override
public void onStartSearch() {
}
#Override
public void onSearch(String search) {
}
#Override
public void onCloseSearch() {
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.your_menu, menu);
// Associate searchable configuration with the SearchView
MenuItem searchItem = menu.findItem(R.id.action_search);
mSearchView = (SearchView) MenuItemCompat.getActionView(searchItem); //here I user appcompat, but you can take it just from actionbarsherlock
SearchView.SearchAutoComplete searchAutoComplete = (SearchView.SearchAutoComplete) mSearchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
searchAutoComplete.setTextColor(getResources().getColor(R.color.action_bar_text_color));
mSearchView.setIconifiedByDefault(true);
mSearchView.setSubmitButtonEnabled(false);
/**
* Set all of different kinds of listeners
*/
MenuItemCompat.setOnActionExpandListener(searchItem, new MenuItemCompat.OnActionExpandListener() {
#Override
public boolean onMenuItemActionCollapse(MenuItem item) {
menuIsOpen = false;
onCloseSearch();
return true;
}
#Override
public boolean onMenuItemActionExpand(MenuItem item) {
menuIsOpen = true;
onStartSearch();
return true;
}
});
mSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextChange(String s) {
onSearch(s);
return false;
}
#Override
public boolean onQueryTextSubmit(String s) {
onSearch(s);
return true;
}
});
super.onCreateOptionsMenu(menu, inflater);
}
/**
* Function for closing search when android is less than 14
*/
public boolean onBackButton() {
if (AndroidUtils.getSdkVersion() >= 14)
return false;
if (menuIsOpen) {
menuIsOpen = false;
getActivity().supportInvalidateOptionsMenu();
onCloseSearch();
return true;
}
return false;
}
After backButton it close search, or just by selecting close from action bar.
You can use a OnActionExpandListener:
public void onCreateOptionsMenu(final Menu menu, final MenuInflater inflater) {
SearchView searchView = new SearchView(((SherlockFragmentActivity) getActivity()).getSupportActionBar().getThemedContext());
searchView.setIconifiedByDefault(true);
// ...
MenuItem menuItem = menu.add(R.string.search);
// ...
menuItem.setOnActionExpandListener(this);
}
#Override
public boolean onMenuItemActionExpand(final MenuItem item) {
mInSearchMode = true;
return true;
}
#Override
public boolean onMenuItemActionCollapse(final MenuItem item) {
mInSearchMode = false;
return true;
}
My requirement is to open a menu when edittext is clicked with options "bold", "italic", "Underline", "Fonts" and "Colors".
Please can someone help me on this..
Here's how I ended up solving this:
EditText menuEdit = (EditText) activity.findViewById(R.id.menuImageView);
menuEdit.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
activity.openOptionsMenu(); //This is the key method!
}
});
public boolean onCreateOptionsMenu(Menu menu) {
com.actionbarsherlock.view.MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.main_menu, menu);
return true;
}
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.demographics:
return true;
case R.id.settings:
Log.v("v", "settings clicked");
return true;
default:
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.