1

I am learning Android developpment. So far I have created a basic application and I would like to display an OptionMenu. I have followed a tutorial. The menu doesn't appear on my phone. Maybe I have forgotten something?

I have looked at this post: Option Menu does not appear in Android but it seems that my problem is different.

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    if (id == R.id.action_refresh) {
        FetchWeatherTask weatherTask = new FetchWeatherTask();
        weatherTask.execute();
        return true;
    }
    return super.onOptionsItemSelected(item);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.forecastfragment, menu);
}

Here is the code of 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:id="@+id/action_refresh"
        android:title="@string/action_refresh"
        app:showAsAction="never" />
</menu>

I have spent hours. Rewritten the code from scratch 2 times but the problem is still here. Everything works well, except that the menu does not appear. What is the problem?

Community
  • 1
  • 1
anothertest
  • 979
  • 1
  • 9
  • 24

2 Answers2

2

And change the app:showAsAction = never to app:showAsAction = ifRoom|withText, add withText to allow users to long press on the items and display the purpose of the items, also if your items are put in the overflow, the text will be displayed instead.

Biu
  • 1,066
  • 10
  • 18
  • Error:(30, 20) error: onCreate(Bundle) in ForecastFragment cannot override onCreate(Bundle) in Fragment return type Boolean is not compatible with void, is the message I get. – anothertest Dec 13 '14 at 20:39
  • See my updated answer, to get a menu inflater call the `getMenuInflater()` of the `Activity` class – Biu Dec 13 '14 at 20:42
  • The OP's code is in a Fragment. The `onCreateOptionsMenu()` method you're referring to is a member of the Activity class. – Mike M. Dec 13 '14 at 20:48
  • my bad, he didnt state it in his question though – Biu Dec 13 '14 at 20:51
  • I didn't realize it was important that my class extended fragments. – anothertest Dec 13 '14 at 20:56
2

in your menu xml chnge last line to

app:showAsAction="ifRoom"

Vigen
  • 483
  • 6
  • 12
  • 1
    if your class extends fragment then you override right functions – Vigen Dec 13 '14 at 20:52
  • Ok now it is appearing. Why wasn't it appearing before? I also tried to change it to always. Why does it seem to appear on some devices without ifRoom? – anothertest Dec 13 '14 at 20:57
  • 1
    Sorry I don't understand question but you can see more about action bar in android documentations http://developer.android.com/guide/topics/ui/actionbar.html – Vigen Dec 13 '14 at 21:03
  • Thank you @Vigen, I start reading it now. I have one last question for you. When I click on the button, it should make the App crash because of a security exeception, but it doesn't. Should I conclude that clicking on the button does nothing in my case? – anothertest Dec 13 '14 at 21:07
  • 1
    You can debug your code to know is click work or not. You also can set toast or log in onOptionsItemSelected function like this "Toast.makeText(getActivity(), "click", 1000).show();" – Vigen Dec 13 '14 at 21:14