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);
}
Related
I have a menu.xml and the code to show the menu in action bar. Do i have to add the code in every activity? Cause after this i use a switch case to get click, and more code, so i dont thing its the wright way to copy-paste the same code in every 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.menu_main, menu);
return true;
Is there a better way to do it as to work in all app (all activities)?
Create a BaseActivity class.
public class BaseActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_main, menu);
return true;
}
}
Now all activities that extend your base activity will have the same menu.
this is a guide for Android Menu
this is an example menu:
<?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" />
<item android:id="#+id/help"
android:icon="#drawable/ic_help"
android:title="#string/help" />
</menu>
And then inflating it within your activity:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.game_menu, menu);
return true;
}
whe item clicked:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.new_game:
newGame();
return true;
case R.id.help:
showHelp();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
good luck and read the doc official for android
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);
}
My meny code is:
<item android:id="#+id/action_map"
android:icon="#android:drawable/ic_dialog_map"
android:title="Add"
android:showAsAction="ifRoom"/>
<item
android:id="#+id/action_settings"
android:showAsAction="never"
android:title="#string/action_settings" />
</menu>
My Fragment code is:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_where_am_i, container, false);
setHasOptionsMenu(true);
return root;
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.where_i_am_menu, menu);
super.onCreateOptionsMenu(menu, inflater);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id) {
case R.id.action_settings:
startActivity(new Intent(getActivity(), SettingsActivity.class));
return true;
case R.id.action_map:
return true;
}
return super.onOptionsItemSelected(item);
}
And i get next action bar. Why i have 2 settings in that list? And i don't want to add label "Add" to that list, hiw can i make it?
You should clear the menu:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
menu.clear();
inflater.inflate(R.menu.where_i_am_menu, menu);
super.onCreateOptionsMenu(menu, inflater);
}
Hope it helps you :)
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);
}