I have created one custom action view layout using MenuItemCompat Java code is as shown in code below :
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
MenuItem shoppingCartitem = menu.findItem(R.id.my_action_item_id);
shoppingCartitem.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
Log.v("MyAPP", "Listener called");
return true;
}
});
MenuItemCompat.setActionView(shoppingCartitem, R.layout.my_custom_action_view_layout);
return super.onCreateOptionsMenu(menu);
}
menu_main.xml as follows :
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:myapp="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity">
<item
android:id="#+id/my_action_item_id"
android:icon="#drawable/ic_shopping_cart_white_24dp"
android:orderInCategory="9999"
android:title="#string/menu"
myapp:showAsAction="always" />
</menu>
What am I missing here, any suggestions ?
From the documentation:
Set a custom listener for invocation of this menu item. In most situations, it is more efficient and easier to use onOptionsItemSelected(MenuItem) or onContextItemSelected(MenuItem).
So you can simply override the onOptionsItemSelected callback, which is easier.
By the way to me your code seems to be right, but it doesn't do anything. Have you tried doing something like Log.v("MyAPP", "Listener called"); inside it?
EDIT:
I wrote this code:
MainActivity.java:
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu, menu);
MenuItem item = menu.findItem(R.id.my_action_item_id);
item.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
Log.v("MyApp", "Inside listener");
return true;
}
});
MenuItemCompat.setActionView(item, R.layout.main_activity);
return super.onCreateOptionsMenu(menu);
}
}
main_menu.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:myapp="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity">
<item
android:id="#+id/my_action_item_id"
android:icon="#drawable/ic_launcher"
android:orderInCategory="9999"
android:title="Menu"
myapp:showAsAction="always" />
</menu>
And this is the result of the LogCat (I clicked the icon twice):
As you can see it works without problems
please try
MenuItem shoppingCartitem = menu.findItem(R.id.my_action_item_id);
change to
MenuItem shoppingCartitem = (MenuItem) findViewById(R.id.my_action_item_id);
Related
I know that this question has been asked before . I tried some of the solutions but nothing is working for me .
What I want in landscape mode is for the searchview to be fully stretched in width and I dont want the keyboard to appear on full screen which makes the UI invisible.
And I want the searchview to be stretched in width
The code below is menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/Search_Box"
android:title="Search"
app:showAsAction="always"
android:icon="#drawable/search_white_24dp"
app:actionViewClass="android.support.v7.widget.SearchView"/>
</menu>
And here is my code
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu,menu);
MenuItem item= menu.findItem(R.id.Search_Box);
searchView = (SearchView) MenuItemCompat.getActionView(item);
if(text!=null && !text.isEmpty())
{
// MenuItemCompat.expandActionView(searchView);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
item.expandActionView();
Toast.makeText(this,"here",Toast.LENGTH_SHORT).show();
}
else {
MenuItemCompat.expandActionView((MenuItem) searchView);
}
searchView.setIconified(false);
searchView.setQuery(text,false);
searchView.clearFocus();
}
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
adapter.filter(newText);
return false;
}
});
return super.onCreateOptionsMenu(menu);
}
please help me where i am wrong
I've been searching for half an hour and none of the solutions for other people are working for me.
Here is what is shows in the preview to the right:
Here is the java code in my activity (I removed the other stuff):
public class PlayGame extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.play_game);
Toolbar myToolbar = (Toolbar) findViewById(R.id.toolbar_playgame);
myToolbar.setTitle("ChessClock");
myToolbar.setTitleTextColor(Color.WHITE);
setSupportActionBar(myToolbar);
}
#Override
public void onBackPressed() { } //Back Button Disabled
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.actionbaroverflow, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_pause:
Toast.makeText(this, "Menu Item 1 selected", Toast.LENGTH_SHORT).show();
break;
}
return true;
}
All the instructions said to make an XML in my 'menu' directory but I didn't have one so I made it (called 'menu' inside of 'res'). The XML 'actionbaroverflow' has this code:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/action_pause"
android:orderInCategory="100"
android:title="Pause"
app:showAsAction="always"/>
And the item is simply not showing up when I run the app. I had an icon but someone said it could have been too big, so I just made it text and it's still not showing up.
Tou need to add to your R.menu.actionbaroverflow
android:icon="#drawable/your_icon"
Will be something like this:
<item
android:id="#+id/action_pause"
android:icon="#drawable/your_icon"
android:orderInCategory="100"
android:title="Pause"
app:showAsAction="always"/>
This works for me fine:
<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:activity=".ProductsNewOrder">
<item android:id="#+id/action_pause"
android:title="Pause"
android:visible="true"
app:showAsAction="always"
android:icon="#drawable/ic_pause"/>
</menu>
and in the activity:
setSupportActionBar(myToolbar);
if (myToolbar!= null) {
myToolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
int id = item.getItemId();
switch (id) {
case R.id.action_pause: {
//do something
break;
}
}
return true;
}
});
}
and where i inflate the menu:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.actionbaroverflow, menu);
return true;
}
Hope it helps!!!
i'm developing an application with API 8. now i'm trying to add 3 items in my menu, one
of item i added is SearchView, i used app:actionViewClass="android.support.v7.widget.SearchView" ofcourse to support the API 8,
and in my MainActivity class in onOptionsItemSelected(MenuItem item) i'm trying to handle the click on searchView, but it is no longer working.
how can fix this?
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">
<!--My SearchView--->
<item
android:id="#+id/menu_search"
android:orderInCategory="100"
android:title="#string/stringSearch"
app:actionViewClass="android.support.v7.widget.SearchView"
android:icon="#drawable/ic_magnify"
app:showAsAction="always"/>
<!--My Filter--->
<item
android:id="#+id/menu_filter"
android:orderInCategory="100"
android:icon="#drawable/ic_filter"
android:title="#string/StringFilter"
app:showAsAction="always" />
<!--Some menu--->
<item
android:id="#+id/menu_settings"
android:orderInCategory="100"
android:icon="#drawable/ic_dots"
android:title="#string/Settings"
app:showAsAction="always" />
</menu>
MainActivity.class
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
...
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
//I setup the searchView here
MenuItem searchItem = menu.findItem(R.id.menu_search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
....
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_search:
//This is click event on searchView but unfortunately not working
Toast.makeText(getApplicationContext(),"SearcView",Toast.LENGTH_LONG).show();
break;
case R.id.menu_settings:
Toast.makeText(getApplicationContext(),"Menu Settings",Toast.LENGTH_LONG).show();
break;
case R.id.menu_filter:
Toast.makeText(getApplicationContext(),"Menu Filter",Toast.LENGTH_LONG).show();
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
}
So, as you can see in my code, i try to fire a toast when the menu item is clicked, everything is working but not my SearchView.
this is happen when i started to add app:actionViewClass="android.support.v7.widget.SearchView" on menu in searchView item.
anyone can help me?
Thank you very much!
You'll want to use one of the listeners on the SearchView class. The OnSearchClickListener looks like it may be the right fit for you.
Edit. Here's a code example:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
MenuItem searchItem = menu.findItem(R.id.menu_search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
searchView.setOnSearchClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Handle clicks
}
});
return super.onCreateOptionsMenu(menu);
}
I created an App that uses Fragment. From my MainActivity I set the ActionBar.
But in one of my Fragment I need to modify the action icons and on click.
So With the code below, when I load the my Fragment, it still display the action bar menu from MainActivity
here is my MainActivity :
public void restoreActionBar() {
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setTitle(mTitle);
// enable ActionBar app icon to behave as action to toggle nav drawer
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeButtonEnabled(true);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
if (!mNavigationDrawerFragment.isDrawerOpen()) {
// Only show items in the action bar relevant to this screen
// if the drawer is not showing. Otherwise, let the drawer
// decide what to show in the action bar.
getMenuInflater().inflate(R.menu.main, menu);
//Handle the Search Menu
SearchView searchView = (SearchView) MenuItemCompat.getActionView(menu.findItem(R.id.action_search));
searchView.setQueryHint(this.getString(R.string.action_search));
((EditText)searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text))
.setHintTextColor(getResources().getColor(R.color.white));
searchView.setOnQueryTextListener(OnQuerySearchView);
mSearchCheck = false;
restoreActionBar();
return true;
}
return super.onCreateOptionsMenu(menu);
}//end onCreateOptionsMenu
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
/** CAMERA **/
case R.id.action_camera:
//openCamera();
Utils.makeToast(getApplicationContext(), "Implement Camera", false);
return true;
/** SEARCH **/
case R.id.action_search:
//openSearch();
mSearchCheck = true;
Utils.makeToast(getApplicationContext(), "Implement Search", false);
return true;
/** SETTINGS **/
case R.id.action_settings:
//openSettings();
Utils.makeToast(getApplicationContext(), "Implement Settings", false);
return true;
/** ABOUT **/
case R.id.action_help:
//openHelp();
Utils.makeToast(getApplicationContext(), "Implement Help", false);
return true;
default:
return super.onOptionsItemSelected(item);
}//end switch
}//end onOptionsItemSelected
private OnQueryTextListener OnQuerySearchView = new OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String arg0) {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean onQueryTextChange(String arg0) {
// TODO Auto-generated method stub
if (mSearchCheck){
// implement your search here
}
return false;
}
};//end OnQueryTextListener
Here is the layout :
<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="com.example.sellbeesclean.MainActivity" >
<!-- CAMERA -->
<item
android:id="#+id/action_camera"
android:orderInCategory="100"
android:icon="#drawable/ic_action_camera"
android:title="#string/action_camera"
app:showAsAction="ifRoom|collapseActionView"/>
<!-- SEARCH -->
<item
android:id="#+id/action_search"
android:orderInCategory="100"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="ifRoom|collapseActionView"/>
<!-- SETTINGS -->
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:title="#string/action_settings"
app:showAsAction="never"/>
<!-- HELP -->
<item
android:id="#+id/action_help"
android:orderInCategory="100"
android:icon="#drawable/ic_action_help"
android:title="#string/action_help"
app:showAsAction="ifRoom|collapseActionView"/> </menu>
Her is my Fragment :
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
setHasOptionsMenu(true);
View rootView = inflater.inflate(R.layout.user_profile_fragment, container, false);
Log.i(TAG, "onCreateView");
.....
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
setHasOptionsMenu(true);
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.fragment_menu, menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
/** EDIT **/
case R.id.action_edit:
//openEditProfile(); //Open Edit Profile Fragment
Utils.makeToast(MyApplication.getAppContext(), "Implement Camera", false);
return true;
default:
return super.onOptionsItemSelected(item);
}//end switch
}//end onOptionsItemSelected
here the fragment menu layout
<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="com.example.sellbeesclean.MainActivity" >
<!-- EDIT -->
<item
android:id="#+id/action_edit"
android:orderInCategory="100"
android:icon="#drawable/ic_action_edit"
android:title="#string/action_edit_profile"
app:showAsAction="ifRoom|collapseActionView"/></menu>
In your fragment's onCreateView method write
setHasOptionsMenu(true);
And inflate your menu xml file in onCreateOptionsMenu method
In onCreateOptionsMenu of a fragment, write
menu.clear();
before inflating menus
I am using ActionBarSherlock Tabs with Fragments in my applications:
public class ExampleActivity extends SherlockFragmentActivity{}
I have added an Options Menu in the Action Bar. The problem is while this does show the options menu on the click of virtual button in the action bar, it doesn't do so when the physical button is pressed. I would like the options menu to be displayed when the physical menu button is pressed.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
//return super.onCreateOptionsMenu(menu);
MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.options_menu, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.menu_prefs:
Intent i = new Intent(this,ShowSettingsActivity.class);
startActivityForResult(i, requestCode);
return true;
case R.id.menu_faq:
startActivity(new Intent(this, AboutApp.class));
return true;
case R.id.menu_contact:
startActivity(new Intent(this, FeedbackApp.class));
return true;
default:
return super.onOptionsItemSelected(item);
}
}
And in res/menu/options_menu.xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/menu_settings"
android:title="#string/menu_settings"
android:icon="#drawable/ic_settings_dark"
android:orderInCategory="100"
android:showAsAction="always">
<menu
android:id="#+id/e">
<item
android:id="#+id/menu_prefs"
android:title="#string/menu_prefs"/>
<item
android:id="#+id/menu_faq"
android:title="#string/menu_faq"/>
<item
android:id="#+id/menu_contact"
android:title="#string/menu_contact"/>
</menu>
</item>
</menu>
If I remove the sub-menu, it displays the options menu on click of physical button.
(Sorry about what my first answer that was off the point)
I don't think that the <menu> XML element accepts android:id as an attribute. Did you tried removing it ?
Another try :
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.options_menu, menu);
return true; // override the useless returned value by super()
}
Try this code:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.options_menu, menu);
return true;
}
Hope this will help you.