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
statements.

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" />
</menu>
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"?>
<resources>

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

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:
            finish();
            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")
    .setIcon(R.drawable.menu_quit_icon);

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

-- 
Thanks,

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" />
</menu>

Functions

// 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){
switch(item.getItemId()){
case R.id.new_client:
addClient();
return true;
case R.id.add_income:
addIncome();
return true;
case R.id.add_expense:
addExpense();
return true;
case R.id.add_mileage:
addMileage();
return true;
}
return false;
}

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


Sponsored Links


Other Threads

1. "Cupcake" Anyone in Google that Could Shed Some Light! :) Thanks!

//Notable changes introduced in cupcake://
#  Music
- New features
          Music playback fades in after suspending for phone call.

# Framework
- Streaming audio I/O for applications.


Questions:

1). Fade Music playback, will we have to lessen to an onCall event?
and handle the fades or will this be something that automatically
fades the music pauses? and after the call resumes playback?

2). Streaming audio, will we be passing an InputStream? that would be
great!


Thanks in advance!
Moto
--~--~---------~--~----~------------~-------~--~----~

2. Apostrophe question

Hi all!

Some question regarding apostrophe as part of string-array items.

I have string array defined as following:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="my_arry">
        <item>Men's info</item>
        <item>Dummy info</item>
</string-array>
</resources>

In code I'm getting the values into string array
String my_array[] = this.getResources().getStringArray
(R.array.my_array_id);
String myFirstString = my_array[0];

The displayed myFirstString is  - "Mens info" without apostrophe.

I changed it to following using ascii code
<string-array name="my_arry">
        <item>Men&#39;s info</item>
        <item>Dummy info</item>
</string-array>

Same result.

Thanks in advance,
Evgeny

--~--~---------~--~----~------------~-------~--~----~

3. Problem getting the width of the root view.

4. PV OpenCORE port connections

5. How to use OCR in android

6. Regarding .apk files

7. html parsing and libraries