In my Activity I have an three icons in Action bar (search - add - count), I want when variable hideIcon is true to hide these icons.
The following is my code.
MainActivity.java
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.activity_main_actions, menu);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.action_search)
.getActionView();
searchView.setSearchableInfo(searchManager
.getSearchableInfo(getComponentName()));
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Take appropriate action for each action item click
switch (item.getItemId()) {
case R.id.action_search:
// search action
return true;
case R.id.action_add_tip:
return true;
case R.id.action_count_tip:
return true;
case android.R.id.home:
return true;
default:
return super.onOptionsItemSelected(item);
}
}
menu.activity_main_actions.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- Search -->
<item android:id="#+id/action_search"
android:icon="#drawable/header_search"
android:title="#string/action_search"
android:showAsAction="ifRoom"
android:actionViewClass="android.widget.SearchView"/>
<!-- Add Tip -->
<item android:id="#+id/action_add_tip"
android:icon="#drawable/header_add"
android:title="#string/action_add_tip"
android:showAsAction="ifRoom" />
<!-- Count Tips -->
<item android:id="#+id/action_count_tip"
android:icon="#drawable/header_count"
android:title="#string/action_count_tip"
android:showAsAction="ifRoom" />
</menu>
How can I hide them if(hideIcon) is true ?
Thanks in advance.
//anywhere in your code
hideIcon = HIDE_MENU; // setting state
invalidateOptionsMenu(); // now onCreateOptionsMenu(...) is called again
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
// inflate menu from xml
MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.settings, menu);
if (hideIcon)
{
for (int i = 0; i < menu.size(); i++)
menu.getItem(i).setVisible(false);
}
}
Related
onOptionsItemSelected(item: MenuItem) is not called for a tap on a menu item with app:actionProviderClass in a project with AndroidX.
Any idea why?
<item
android:id="#+id/open_map"
android:orderInCategory="100"
android:title="#string/map"
app:showAsAction="always"/>
<item
android:id="#+id/add_player"
android:orderInCategory="100"
android:title="#string/add_player"
android:icon="#drawable/ic_add_white_24dp"
app:showAsAction="ifRoom"/>
<item
android:id="#+id/share"
android:orderInCategory="100"
android:title="#string/share"
app:actionProviderClass="androidx.appcompat.widget.ShareActionProvider"
app:showAsAction="ifRoom"/>
You used app:actionProviderClass="androidx.appcompat.widget.ShareActionProvider"
Use this same as search action view
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.example, menu);
this.menu = menu;
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
SearchManager manager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView search = (SearchView) menu.findItem(R.id.search).getActionView();
search.setSearchableInfo(manager.getSearchableInfo(getComponentName()));
search.setOnQueryTextListener(new OnQueryTextListener() {
#Override
public boolean onQueryTextChange(String query) {
loadHistory(query);
return true;
}
});
}
return 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 used SherlockActionBar for my ActionBar, but I want to use AppCompact for my search view. To change my code from sherlock to AppCompact, I wrote this:
#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_main, menu);
// SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
SearchView searchView = (SearchView) MenuItemCompat.getActionView(menu.findItem(R.id.action_search));
//searchView.setOnQueryTextListener(this);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String s) {
return false;
}
#Override
public boolean onQueryTextChange(String s) {
return false;
}
});
return true;
// return super.onCreateOptionsMenu(menu);
}
xml menu:
<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=".MainActivity">
<item
android:id="#+id/action_search"
android:actionViewClass="android.support.v7.widget.SearchView"
android:icon="#android:drawable/ic_menu_search"
android:title="action_search"
app:showAsAction="always|collapseActionView" />
</menu>
but it crashed. How can I fix it?
EDIT:
change android:actionViewClass to app:actionViewClass
My menu is not showing in the ActionBar (onCreateOptionsMenu) is called properly. The icon and string are available. The code works fine in my other projects. I am using android.support.v7.app.ActionBarActivity for android:
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="21" />
Code:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
Log.i("onCreate", "menu");
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.add, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_add:
Intent addIntent = new Intent(this, RoomAddActivity.class);
startActivity(addIntent);
break;
}
return super.onOptionsItemSelected(item);
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_add"
android:icon="#drawable/ic_action_new"
android:showAsAction="always"
android:title="#string/action_add"/>
</menu>
Try to return true explicitly:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
Log.i("onCreate", "menu");
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.add, menu);
super.onCreateOptionsMenu(menu);
return true;
}
Updated:
By the way, you are not obliged to call super method. As to my experience it works all right without it.
In onCreateOptionsMenu I'm setting an action view with:
MenuItem item = ..
item.setActionView(action_view)
item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
and it works ok. But I want to move this to my xml menu and I can't seem to make it show. It only shows up the title. I tried many versions, like:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourApp="http://schemas.android.com/apk/lib/myPackage.myClass" >
<item
android:id="#+id/item"
yourApp:actionViewClass="action_view"
android:showAsAction="always" or youtApp:showAsAction="always"
android:title="title"
</item>
The problem must be it getting to the action_view variable, but I can't seem to figure it out
Did you inflate the menubar?
#Override
public void onCreateOptionsMenu (Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_name, menu);
mMenu = menu;
if(mMenu != null){
MenuItem item = mMenu.findItem(R.id.item_id);
if(item != null){
item.setVisible(false);
}
}
super.onCreateOptionsMenu(menu, inflater);
}
Also do you have setHasOptionsMenu(true); #onCreate() function?
Try this:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.item:
// Write your action_view here
break;
case R.id.your_other_item_id:
//Your other Menu item logic
break;
}
return super.onOptionsItemSelected(item);
}