Hi Below is the code I am using to create option menu in my FragmentActivity :-
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
// Menu options to set and cancel the alarm.
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// When the user clicks START ALARM, set the alarm.
case R.id.start_action:
alarm.setAlarm(this);
return true;
// When the user clicks CANCEL ALARM, cancel the alarm.
case R.id.cancel_action:
alarm.cancelAlarm(this);
return true;
}
return false;
}
Will anybody tell me why it's not working? It is not affecting app but nothing is happening when I click the option menu button from device. Please Help to resolve this. Thanks in advance!
Below is my main.xml :-
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/start_action"
android:showAsAction="ifRoom|withText"
android:title="@string/start_text" />
<item android:id="@+id/cancel_action"
android:showAsAction="ifRoom|withText"
android:title="@string/cancel_text" />
</menu>
Although this question is old but to close it here is what i believe OP is missing in the code
In the
return it with the super as super.onCreateOptionsMenu(menu);
and in the
return it with the super as super.onOptionsItemSelected(item);
all the return type is boolean so you will know it worked correctly when it returns true. Its like simillar to super.onCreate(savedInstancestate).
Change
to
as
Edit:
Also you have to add the following to your
Fragment
Return item within Switch case like. ITs Work For me.