Reputation: 5651
I am new to Java and Android development and I am trying to use event handlers for menus. I had no problems setting up the menus in xml, but now I am confused on how to use handlers.
I am using
onOptionsItemSelected(MenuItem item)
and I only know how to create new activities, eg:
startActivity(new Intent(this,About.class))
I've seen many tutorials but they all focus on buttons, which I tried and failed. Also, can I have methods in different classes to better organize my code? For example have method1.java, method2.java, method3.java,.... and instantiate these classes to call on the methods.
If it helps, what I am trying to do is use OpenGL and allow the user to be able to rotate, translate, resize, etc depending on the menu option selected.
EDIT: I am trying to use states for my program, and only be able to use the handlers defined for the given states.
Upvotes: 2
Views: 9211
Reputation: 48871
In the menu xml file, each item has an id, example...
<item
android:id="@+id/reset"
android:title="@string/gla_menu_title_reset" />
In your onOptionsItemSelected(MenuItem item)
handler you need to get the id of the item that is passed in and then process it. An easy way of doing this is with a switch / case...
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.reset:
// Do something
return true;
case R.id.something_else:
...
return true;
}
}
And, yes, you can create standard Java classes in Android.
Upvotes: 3