A few menu questions...

by Andy McCall » Sat, 21 Feb 2009 01:49:28 GMT

Sponsored Links
 Hi Folks,

I have been working working with Eclipse, the Android plug-in and the 1.1
SDK.  So far I quite like it and its bringing back memories of the BeOS's
API :)  I generally learn by doing, and I am stuck with some menu problems.
I want to create my menus using and XML file, and I can do that, but I can't
work out how to link up my menu values and the XML values for my case

I have the following XML file, res/menu/options_menu.xml:

<menu xmlns:android=" http://schemas.android.com/apk/res/android" ;>
 <item android:id="@+id/options_menu_settings"
    android:title="Settings" />
 <item android:id="@+id/options_menu_about"
          android:title="About" />
    <item android:id="@+id/options_menu_quit"
          android:title="Quit" />
Creating the menu by inflating it using the following:

    public boolean onCreateOptionsMenu(Menu menu) {
     MenuInflater inflater = getMenuInflater();
     inflater.inflate(R.menu.options_menu, menu);
        return true;

So far this works and draws the menu.

I have the following file res/values/strings.xml:

<?xml version="1.0" encoding="utf-8"?>

<item name="options_menu_quit" type="id" format="integer">0</item>

But how do I get the case statement and the XML to fit?  I would have
expected it to be something like this:

    /* Handles item selections */
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case options_menu_quit:
            return true;
        return false;

But that doesn't work as the options_menu_quit can't be resolved.

My other question is about menu icons. I get that this code adds an icon to
the menu:

menu.add(0, MENU_QUIT, 0, "Quit")

But if I am drawing a menu by inflating XML, how do I specify an icon there?


Andy McCall
Email: andy.mcc...@gmail.com
GTalk: andy.mcc...@gmail.com


A few menu questions...

by sddandroid » Mon, 16 Mar 2009 04:42:07 GMT

 ndy, I'm not sure if you ever found an answer to your question but,
the solution (IMHO) needs to be posted because it isn't elaborated on
in the Dev documentation or on the Developers site (anywhere I could
find). If you write up your menus using XML, you can access the item
ids using R.id like this....

XML File
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/new_client"
android:title="New Client" />
<item android:id="@+id/add_income"
android:title="Add Income" />
<item android:id="@+id/add_expense"
android:title="Add Expense" />
<item android:id="@+id/add_mileage"
android:title="Add Mileage" />


// Called when the menu button is pressed
public boolean onCreateOptionsMenu(Menu menu){
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.options_menu, menu);
return true;

// Handles when menu options are selected
public boolean onOptionsItemSelected(MenuItem item){
case R.id.new_client:
return true;
case R.id.add_income:
return true;
case R.id.add_expense:
return true;
case R.id.add_mileage:
return true;
return false;

On Feb 20, 9:33am, Andy McCall <andy.mcc...@gmail.com> wrote:

Sponsored Links

Other Threads

1. Windows Phone and XNA. Nightmare is real. What we do with that ?

While it's not nearly as full featured as XNA i started working on
something similar to XNA. It allows you to develop your games mostly
on the desktop and deploying it to your Android device with just a
couple of lines that instantiate a special Activity subclass. It's
based on OpenGL and allows developing 2D and 3D games. I just finished
writting all the java doc for it and am constantly adding new features
to it. It's called libgdx and can be found at  http://www.***.com/ 
I also started blogging about it lately and will continue so adding
sample codes for specific problems. You can find that blog at
 http://www.***.com/ . An introduction to it can be found at
series of small tutorials wil follow this week and next week.

 The whole thing is LGPL so that there's no problem including it in
commercial apps. It's far from being perfect of course but i think the
base functionality and ease of use can kill some of the burden a fresh
android game developer has to overcome.

I'm open for suggestions and features you want to see in there!


2. My location for Google map

Hi All,
    I downloaded Google map on my Android 2.0 platform. If i trying to
use My location function. Have error log message as below list , Have
anyone know what's reason???

E/LocationManagerService( 1783): getLastKnownLocation got exception:
E/LocationManagerService( 1783): java.lang.IllegalArgumentException:
E/LocationManagerService( 1783):        at
E/LocationManagerService( 1783):        at
E/LocationManagerService( 1783):        at android.location.ILocationManager
E/LocationManagerService( 1783):        at
E/LocationManagerService( 1783):        at
com.android.server.SystemServer.init1(Native Method)
E/LocationManagerService( 1783):        at
E/LocationManagerService( 1783):        at
java.lang.reflect.Method.invokeNative(Native Method)
E/LocationManagerService( 1783):        at
E/LocationManagerService( 1783):        at com.android.internal.os.ZygoteInit
E/LocationManagerService( 1783):        at
E/LocationManagerService( 1783):        at
dalvik.system.NativeStart.main(Native Method)

unsubscribe: android-porting+unsubscr...@googlegroups.com

3. FLAG_ONLY_ALERT_ONCE not working in notification at all

4. Google login service crashing

5. Help regarding Logging

6. Basic program structure questions

7. The auto-generated R.java generation is invalid. Can't compile a new APK.