Hi i'm trying to show the menu when the user presses the menu button. I'm using the code from the Documentation but the options menu won't show up. I guess i should have a listener for this menu button, but how?? This is my class so far:
public class AppMenu extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.about);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.appmenu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.hello:
sayHello();
return true;
case R.id.bye:
finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
Here is my xml file
<?xml version="1.0" encoding="UTF-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/hello"
android:title="Hello"
android:icon="@drawable/icon"/>
<item android:id="@+id/bye"
android:title="Bye" />
</menu>
Thanks !
Now it's working as it is supposed to work. I just implemented this: link text thanks for the patience.
This answer is in response to the comment discussion on the question.
You can't have to menu appear outside of your Activity
. That means you have to start your Activity
and then from inside your Activity
you'll be able to get the menu appear on a menu button press.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.