Reputation: 375
I created a simple activity (with a menu) and tried adding menu items but they don't show up when trying to actually give them some function in the onOptionsItemSelected() method. I'm not sure why it's not working, as I did the exact same thing in the menu for the main activity and it worked just fine. When typing in android.R.id.add_screen_submit_button for example, it is not recognized as existing. And if I forcefully just type it in and leave it the message "add_screen_submit_button cannot be resolved or is not a field" comes up. The menu is also in the correct folder (I actually just left it as is when creating the Activity). Thanks in advance.
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/add_screen_submit_button"
android:orderInCategory="4"
android:showAsAction="always"
android:title="@string/add_screen_ok"
android:icon="@drawable/accept_icon" />
<item
android:id="@+id/add_screen_cancel_button"
android:orderInCategory="5"
android:showAsAction="always"
android:title="@string/add_screen_cancel"
android:icon="@drawable/cancel_icon" />
</menu>
Here's the code
public class AddActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add);
// Show the Up button in the action bar.
//setupActionBar();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.add_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
case android.R.id.add_screen_submit_button:
Toast.makeText(this, "Map Selected", Toast.LENGTH_SHORT).show();
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
}
Upvotes: 1
Views: 2811
Reputation: 44571
Change
android.R.id.home:
to
R.id.home:
and the same with the other one.
android.R
is for sdk resources and R.id.some_id
is for id
s you create
Upvotes: 3