Sunday, September 11, 2011

Android Programming - Override Options Menu

To override options menu simply add this code to your activity.

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.listmenu, menu);
        return true;
    }



@Override
    public boolean onMenuItemSelected(int featureId, MenuItem item) {
        switch (item.getItemId()) {
        case R.id.about:
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setMessage("Overriding Options Menu\n(c) 2011")
                   .setTitle("Menu Pressed")
                   .setCancelable(true);
            builder.create().show();
            return true;
        }
        return super.onMenuItemSelected(featureId, item);
    }

The first section is the overriding of menu button, whenever menu button pressed, it will call you menu from listmenu.xml that placed in the folder res/menu.
Then the second section is the function that run when user click your options menu. For this code, I will create an AlertDialog that shows some text with close button in it.
Don't forget to create your listmenu.xml. Here is the example.
<?xml version="1.0" encoding="utf-8"?>
<menu
  xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/about" android:title="About"></item>
</menu>

Categories: , , , , ,

Related Posts:

  • Android Programming - Send SMSAndroid is for mobile, so it must preserve support to mobile function like calling and SMS. In Android SMS can be done by starting SMS activity class via Intent or create yourself a SMS Sending class. I'll create my own metho… Read More
  • Android Programming - Override Options MenuTo override options menu simply add this code to your activity. @Override     public boolean onCreateOptionsMenu(Menu menu) {         MenuInflater inflater = getMenuInflater(); &n… Read More
  • Android Programming - Override Back ButtonOverriding back button is much more simple than options menu before. @Override     public void onBackPressed() {         AlertDialog.Builder builder = new AlertDialog.Builder(this… Read More
  • Android Programming - Url ConnectThis the basis for web service. Android + Web Service is a future will be for me. With open platform you can access open information. That's really great I think. Simple, with a piece of code to access web service in Android,… Read More
  • Android Programming - Setting Up Your EnvironmentThis is my first article of Android Programming Series in my blog. I'll share you how to set up an environment to begin programming in Android. Actually it was easy and the Android Developer site have a full article about thi… Read More

0 komentar:

Copyright © 2025 Johannes Dwi Cahyo | Powered by Blogger

Design by Anders Noren | Blogger Theme by NewBloggerThemes.com | BTheme.net      Up ↑