Menu showing but onCreateOptionsMenu() not called - java

The menu is visible in the Toolbar of the application and I can open it and see the menu items, however the onCreateOptionsMenu() function is not being called, I know this by setting a break point and debugging, the same goes for the onOptionsItemSelected() function, it is not called at all. I have looked at other stack overflow posts about the same problems but I don't seem to have done any of the usual errors. Does anyone know what the issue is and how to fix it?
MainActivity.java
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId())
{
case R.id.menuItem1:
Toast.makeText(this, "menuItem1 selected", Toast.LENGTH_SHORT).show();
return true;
case R.id.menuItem2:
Toast.makeText(this, "menuItem2 selected", Toast.LENGTH_SHORT).show();
return true;
case R.id.menuItem3:
Toast.makeText(this, "menuItem3 selected", Toast.LENGTH_SHORT).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
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/menuItem1"
android:title="One"
app:showAsAction="never"/>
<item
android:id="#+id/menuItem2"
android:title="Two"
app:showAsAction="never"/>
<item
android:id="#+id/menuItem3"
android:title="Three"
app:showAsAction="never"/>
</menu>
Edit:
In my onCreate() in MainActivity.java I have this line of code:
toolBar.inflateMenu(R.menu.menu);
If I remove this line, the menu in the Toolbar disappears. Don't know if this helps or has anything to do with my issue, but just putting out there so you know.

Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);

In oncreate function, put this line:
setSupportActionBar(bottomAppBar)
See this code for more details:
https://github.com/ranger163/BottomAppBarImplementation

setSupportActionBar(toolBar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
and using android.support.v7.widget.Toolbar would work

Related

My app has an action bar, but no menu.xml. How do I modify my action bar?

Hello and thanks for reading,
When I first made my project, I was prompted by Android Studio to chose a boiler plate. I chose empty activity (the one without FAB and others). Still, my app has an ActionBar, but it just shows the name. Now, I want to modify that action bar and add a menu. My java extends AppCompatActivity, so there is an action bar. However, unlike my prior experiences in eclipse, there is no menu xml to that I can locate.
How can I add one or modify my action by through other means? Can I add one manually?
Thanks!
1) You need to create (or modify if it exist) your menu resources file, /res/menu/main_menu.xml to create the actions.
eg:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_refresh"
android:showAsAction="always"
android:icon="#drawable/ic_action_refresh"
android:title="Refresh"/>
<item
android:id="#+id/action_settings"
android:showAsAction="always"
android:icon="#drawable/ic_action_setting"
android:title="Settings">
</item>
</menu>
2) Override onCreateOptionsMenu() in your activity to allows to inflate actions defined in your XML:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_menu, menu);
return true;
}
3) Override onOptionsItemSelected() to react the actions selection:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_refresh:
Toast.makeText(this, "Refresh selected", Toast.LENGTH_SHORT).show();
break;
case R.id.action_settings:
Toast.makeText(this, "Settings selected", Toast.LENGTH_SHORT).show();
break;
}
return true;
}

Unable to add action bar items in Android Studio

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!!!

Android Actionbar SearchView with item.getItemId() not working

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

Creating a New button Android

I am trying to create a New button (icon only) next to the 3 dots button in the ActionBar. This is the code I tried, but it adds it as child of the 3 dots button.
main_activity_actions.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/create_game"
android:menuCategory="container"
android:orderInCategory="1"
android:title="#string/new_game"
android:titleCondensed="nieuw spel">
</item>
</menu>
MainActivity.java:
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_activity_actions, menu);
return true;
}
Also, how can I find out what default icons I can use?
To show icons in the action bar, you can add the line android:showAsAction="always" to your XML item. Still Android would show it only if it judges there is adequate space in the action bar. If you strictly always want to show your button, you can define and set a custom layout for your action bar.
You can download the default icon set for action bar from https://developer.android.com/design/downloads/index.html
try it
/res/menu/activity_main.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/menu_settings"
android:showAsAction="never"
android:title="#string/menu_settings"/>
<item
android:id="#+id/menu_save"
android:showAsAction="ifRoom"
android:icon="#android:drawable/ic_menu_save"
android:title="#string/menu_guardar"/>
<item
android:id="#+id/menu_new"
android:showAsAction="ifRoom|withText"
android:icon="#android:drawable/ic_menu_add"
android:title="#string/menu_nuevo"/>
</menu>
MainActivit.class
public class MainActivity extends Activity {
//...
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
//onClic item
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_new:
Log.i("ActionBar", "Nuevo!");
return true;
case R.id.menu_save:
Log.i("ActionBar", "Guardar!");;
return true;
case R.id.menu_settings:
Log.i("ActionBar", "Settings!");;
return true;
default:
return super.onOptionsItemSelected(item);
}
}
And you can download All IconPack here:
https://developer.android.com/design/downloads/index.html
Yes, you can inflate a custom actionbar for adding buttons with images like this:
#Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LayoutInflater inflater = (LayoutInflater) getActionBar()
.getThemedContext()
.getSystemService(LAYOUT_INFLATER_SERVICE);
View customActionBarView = inflater.inflate(R.layout.actionbar_custom, null);
ActionBar actionBar = getActionBar();
actionBar.setDisplayOptions(
ActionBar.DISPLAY_SHOW_CUSTOM,
ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_SHOW_TITLE);
actionBar.setCustomView(customActionBarView,
new ActionBar.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));
setContentView(R.layout.activity_main);
}
Where _actionbar_custom.xml_ would be your layout resource, usually a LinearLayout with whatever components you want.

Physical menu button doesn't show options

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.

Categories