I want to add buttons in Actionbar of Android Support library.
Actionbar is shown, but no button appears.
Below is res/menu/main.xml
menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:showAsAction="always"
android:icon="#android:drawable/ic_menu_preferences"
android:title="#string/action_settings"/>
</menu>
and I add following code.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
MenuInflater inflater=getMenuInflater();
inflater.inflate(R.menu.main, menu);
return true;
}
What should I do??
try this:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
Inside a Fragment:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater)
{
inflater.inflate(R.menu.main, menu);
super.onCreateOptionsMenu(menu, inflater);
}
Also, remember to set setHasOptionsMenu(true); in your onActivityCreated method.
In an Activity:
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
Related
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);
}
I have this fragment connected with a menu.xml. I the menu opens when i click it, everything works besides the onOptionsItemSelected. I've tried to log it but nothing gives result. Why isn't my code working?
Activity
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
// TODO Add your menu entries here
super.onCreateOptionsMenu(menu, inflater);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId())
{
case R.id.clear:
writetofile("0", "file1.txt");
writetofile("0", "file2.txt");
writetofile("0", "file3.txt");
break;
case R.id.add:
writetofile("1", "file1.txt");
writetofile("1", "file2.txt");
writetofile("1", "file3.txt");
break;
}
return true;
}
XML
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/clear"
android:orderInCategory="100"
android:title="Clean"/>
<item
android:id="#+id/add"
android:orderInCategory="100"
android:title="Add"/>
You didn't inflate the menu
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.menu, menu);
}
i have menu.xml like this:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/settings"
android:alphabeticShortcut="#string/settings_shortcut"
android:icon="#drawable/setting"
android:title="#string/settings"/>
<item
android:id="#+id/info"
android:alphabeticShortcut="#string/info_shortcut"
android:icon="#drawable/info"
android:title="#string/info"/>
</menu>
Now we usually do it like this:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
But how can i do this programatically ? without using xml
you can use Menu.add
For example, to create a "New" menu with an id
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
menu.add(0, id, 0, "New");
return true;
}
I have added a simple menu but its not showing up.
this is menu.xml file-
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/settings"
android:alphabeticShortcut="#string/settings_shortcut"
android:title="#string/settings_label"></item>
</menu>
this is main.java file-
#Overridek
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
I just wanted to keep it simple.
Inflate your menu options in your .java file through this -
// Initiating Menu XML file (menu.xml)
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.layout.menu, menu);
return true;
}
Hope this helps you lot.
You should delete this line : super.onCreateOptionsMenu(menu);
#Override
public boolean onCreateOptionsMenu(Menu menu) {
//super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}