this is the xml file :
<?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/main_main_prefs"
android:icon="#drawable/ic_tune_white_24px"
android:title="#string/menu_settings"
android:visible="true"
app:showAsAction="always" />
</menu>
But this is the case in actual device:
Update: this is the menu inflation code. And the picture is the response in real device.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
(new MenuInflater(this)).inflate(R.menu.menu_main,menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.main_main_prefs:
Toasty.info(this,"Prefs Was Clicked").show();
break;
}
return super.onOptionsItemSelected(item);
}
You have to create menu like this
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return true;
instead of
(new MenuInflater(this)).inflate(R.menu.main,menu);
return super.onCreateOptionsMenu(menu);
You can try adding android:orderInCategory. Something like this:
<?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/main_main_prefs"
android:icon="#drawable/ic_tune_white_24px"
android:title="#string/menu_settings"
android:orderInCategory="100"
android:visible="true"
app:showAsAction="always" />
</menu>
Related
How do I implement an option menu in my android application? I tried code from Android Developer but I get errors. Such as these: Element menu must be declared. Here is my code
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/new_game"
android:icon="#drawable/ic_new_game"
android:title="#string/new_game"
android:showAsAction="ifRoom"/>
<item android:id="#+id/help"
android:icon="#drawable/ic_help"
android:title="#string/help" />
</menu>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.lucavanraalte.test" >
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme" >
<activity android:name=".MainActivity" android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
In your java code, add this onCreateOptionsMenu to show optionMenu,
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.option_menu, menu); //your file name
return super.onCreateOptionsMenu(menu);
}
Keep your under res\menu\option_menu folder,
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/new_game"
android:icon="#drawable/ic_new_game"
android:title="#string/new_game"
android:showAsAction="ifRoom"/>
<item android:id="#+id/help"
android:icon="#drawable/ic_help"
android:title="#string/help" />
</menu>
Now, if you want to set onOptionsItemSelected i.e onClick event for that ou can use,
#Override
public boolean onOptionsItemSelected(final MenuItem item) {
switch (item.getItemId()) {
case android.R.id.new_game:
//your code
// EX : call intent if you want to swich to other activity
return true;
case R.id.help:
//your code
return true;
default:
return super.onOptionsItemSelected(item);
}
}
You should use onCreateOptionsMenu (Menu menu)
Initialize the contents of the Activity's standard options menu. You
should place your menu items in to menu.
This is only called once, the first time the options menu is
displayed. To update the menu every time it is displayed, see
onPrepareOptionsMenu(Menu).
onCreateOptionsMenu(Menu menu) method which needs to override in Activity class. This creates menu and returns Boolean value. inflate inflates a menu hierarchy from XML resource.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.option_menu, menu); // set your file name
return super.onCreateOptionsMenu(menu);
}
Your option_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/item_First"
android:title="#string/item_First"
android:showAsAction="ifRoom"/>
<item android:id="#+id/save_menu"
android:title="#string/save"
android:showAsAction="ifRoom"/>
<item android:id="#+id/item_Second"
android:title="#string/item_First"
android:showAsAction="ifRoom"/>
</menu>
Please check demo Android Option Menu Example
You need to create a menu folder in the res directory and in the menu directory create file named my_menu.xml. In that file write these lines:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/new_game"
android:icon="#drawable/ic_new_game"
android:title="#string/new_game"
android:showAsAction="ifRoom"/>
<item android:id="#+id/help"
android:icon="#drawable/ic_help"
android:title="#string/help" />
</menu>
Then in your Activity, do this:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.my_menu, menu);
return true;
}
You need to create a menu.xml in directory res->menu like menu
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/new_game"
android:icon="#drawable/ic_new_game"
android:title="#string/new_game"
android:showAsAction="ifRoom"/>
<item android:id="#+id/help"
android:icon="#drawable/ic_help"
android:title="#string/help" />
</menu>
Then you need to create your menu from activity with below code
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.help) {
//do something
return true;
}
if (id == R.id.new_game) {
//do something
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add("Home");
menu.add("Profile");
menu.add("Settings");
menu.add("Privacy Policy");
menu.add("Terms of use");
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(#NonNull MenuItem item) {
if (item.getTitle()=="Home"){
Toast.makeText(this, "Home option selected", Toast.LENGTH_SHORT).show();
}else if (item.getTitle()=="Profile"){
Toast.makeText(this, "Profile option selected", Toast.LENGTH_SHORT).show();
}else if (item.getTitle()=="Settings"){
Toast.makeText(this, "Settings option selected", Toast.LENGTH_SHORT).show();
}else if (item.getTitle()=="Privacy Policy"){
Toast.makeText(this, "Privacy Policy option selected", Toast.LENGTH_SHORT).show();
}else if (item.getTitle()=="Terms of use"){
Toast.makeText(this, "Terms of use option selected", Toast.LENGTH_SHORT).show();
}
return super.onOptionsItemSelected(item);
}
I write an option menu code with a tutorial.
and for some reason it didnt work.
my checker_page_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:MyApp="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/waiters_filtering"
android:title="#string/waiters"
MyApp:showAsAction="always"
/>
<item android:id="#+id/areas_filtering"
android:title="#string/waiters"
MyApp:showAsAction="always"
/>
<item android:id="#+id/tables_filtering"
android:title="#string/waiters"
MyApp:showAsAction="always"
/>
<item android:id="#+id/tapas_filtering"
android:title="#string/waiters"
MyApp:showAsAction="always"
/>
</menu>
and my totaly normal OnCreatOptinMenu in the MainActivity:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.checker_page_menu, menu);
return true;
}
What seems to be the problem and why cant i see my optionMenu?
I tried
#Override
public boolean onCreateOptionsMenu(Menu menu) {
Log.v(
this.getClass().getName() + "!!!",
new Exception().getStackTrace()[0].getMethodName()
);
MenuItem m_item = (MenuItem)menu.findItem(R.id.action_settings);
if(m_item != null)
m_item.setTitle("Back to test");
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.settings, menu);
return true;
}
but it always gets null.And also,onCreate seems to have it as null as well. Is there a function that i can modify the text on it during runtime??? And if so, is there an easy way to find it?
You should add items in the menu xml. You can find it on the folder res/menu. There you have all the menus available for inflation.
I suppose that you have only one. This is how it could look with added options:
<?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/searchMain"
android:icon="#drawable/ic_action_search"
app:showAsAction="ifRoom|withText"
android:title="Search"/>
<item
android:id="#+id/searchBarcodeScan"
android:icon="#drawable/ic_launcher"
app:showAsAction="always|withText"
android:title="Scanner"/>
<item
android:id="#+id/seeList"
android:icon="#drawable/ic_launcher"
app:showAsAction="ifRoom|withText"
android:title="See list"/>
<item
android:id="#+id/settings"
android:icon="#drawable/ic_settings"
app:showAsAction="ifRoom|withText"
android:title="Settings"/>
</menu>
Then in your activity you can react to the option selected by overriding this method and doing things inside it:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.searchMain:
doSomething();
return true;
case R.id.searchBarcodeScan:
doSomething2();
return true;
case R.id.seeList:
doSomething3();
return true;
case R.id.settings:
doSomething4();
return true;
}
return super.onOptionsItemSelected(item);
}
EDIT
In order to change menus in runtime, you should call the method invalidateOptionsMenu(). This method will force the recreation of the menu and this time onPrepareOptionsMenu method will be called. You should override it in your Activity this way:
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
if(someCondition){
getMenuInflater().inflate(R.menu.main_menu, menu);
}
else if(someOtherCondition){
getMenuInflater().inflate(R.menu.other_menu, menu);
}
return true;
}
Instead of
getMenuInflater().inflate(R.menu.settings, menu);
do something like:
getMenuInflater().inflate(R.menu.menu_custom, menu);
where res/menu/menu_custom.xml is something like:
<?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/menu_search"
android:icon="#drawable/ic_action_search"
android:title="#string/search"/>
<item android:id="#+id/menu_settings"
android:icon="#drawable/ic_action_settings"
android:title="#string/settings" />
</menu>
This will give you two items in the dropdown. It looks like your res/menu/settings.xml only has one item in it.
For some reason, I cannot get my button to appear on the Action Bar. I have defined it in an XML file in /res/menu, along with inflating it and listening for an action. The icon is present in /res/drawable-hdpi. And nothing of interest shows in logcat. :(
XML:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/logout"
android:icon="#drawable/logout"
android:title="Logout"
android:orderInCategory="100"
android:showAsAction="always" />
</menu>
Code in main activity:
public class MainActivity extends ActionBarActivity {
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_activity_actions, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case R.id.logout:
//logout code
return true;
default:
return super.onOptionsItemSelected(item);
}
}
//rest of app
}
I followed this question for my initial problem, and it didn't help. How to add button in ActionBar(Android)?
Try with this change:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto" >
<item android:id="#+id/logout"
android:icon="#drawable/logout"
android:title="Logout"
android:orderInCategory="100"
yourapp:showAsAction="always" />
</menu>
How to show icon with option menu.I have tried the following code but my option menu is without image icon.I am using android version 4.0 for developing app.
Java code :
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
menu.add("Add Contacts").setIcon(
R.drawable.ic_launcher);
return true;
}
Following is my app's screen shot
I need image to be displayed on the top of "Add Contacts" item.
Override OnPrepareOptionsMenu and add icon from there too
and if its for above 3.0, use android:showAsAction in xml.
eg. android:showAsAction="ifRoom|withText"
I tried the code in two line and it works:
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
menu.add("Add Contacts");
menu.getItem(0).setIcon(R.drawable.ic_launcher);
return true;
}
You can create a custom menu like this:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/add_contacts"
android:icon="#drawable/ic_launcher"
android:title="#string/add_contacts"
/>
</menu>
And then inflate it
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.my_menu, menu);
return true;
}
More on this here:
http://developer.android.com/guide/topics/ui/menus.html#options-menu
you can directly set this into the xml file.
<item android:id="#+id/add_contacts"
android:icon="#android:drawable/plus_icon"
android:title="Add Contacts"/>
You Can try Following this Link.
Check this out and tell me if it worked or not.
Or you can do some thing like this.
Create menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/next"
android:icon="#drawable/ic_next"
android:title="#string/next" />
<item android:id="#+id/previous"
android:icon="#drawable/ic_previous"
android:title="#string/previous" />
<item android:id="#+id/list"
android:icon="#drawable/ic_list"
android:title="#string/list" />
</menu>
And now you will be able to set ICON on menu
Now in CreateOptionMenu
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
And to access that menu.
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.next:
Toast.makeText(this, "You have chosen the " + getResources().getString(R.string.next) + " menu option",
Toast.LENGTH_SHORT).show();
return true;
…
default:
return super.onOptionsItemSelected(item);
}
}
To enable Option Menu with Icons:
Wrap the Items with an Item with showAsAction="always" and a Menu:
<?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:title="#string/title_menu"
android:icon="#mipmap/ic_icon_menu"
app:showAsAction="always">
<menu>
<item
android:id="#+id/action1"
android:orderInCategory="100"
android:title="#string/action1"
android:icon="#mipmap/ic_icon_action1"
app:showAsAction="never" />
</menu>
</item>
</menu>
If you use some following attribute in manifest file then it's will be show your icon....
<activity android:name=".ui.CategoryActivity"
android:label="#string/app_name"
**android:theme="#android:style/Theme.NoTitleBar"**></activity>
It's work fine for me...:)
+1 for my own effort...
**must be enter.
Easiest way is to use the #drawable only when setting your menu item.
OR
Simply put the #drawable before the title declaration.
<?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/addToFavorites"
android:icon = "#drawable/ic_favorite_border_white_24dp"
android:title = "Hello"
app:showAsAction="always" />
<item
android:id ="#+id/about"
android:title ="About"
app:showAsAction="never" />
</menu>
the problem is the Androidmanifest.xml.
Remove android:theme="#style/AppTheme" and it will work just fine