I have found the solution.  Perhaps this may help others in creating
an OPTIONS MENU in their application.

Here's the code:

package com.calculator;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class Calculator extends Activity {

    private static final int MY_SETUP = 0;                        //
NOTE: ADD THESE TWO LINES FOR YOUR OPTIONS MENU
    private static final int MY_ABOUT = 1;

        /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

----  code of the project -----

    // options menu

    // Called only the first time the options menu is displayed.
    // Create the menu entries.
    // Menu adds items in the order shown.
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
 
menu.add("Setup")                                                     //
ADD LABEL
        .setIcon(android.R.drawable.ic_menu_preferences);      // ADD
ICON
 
menu.add("About")                                                     //
ADD LABEL
        .setIcon(android.R.drawable.ic_menu_info_details);      // ADD
ICON
        return true;
    }

    // handle menu selected
    public boolean onOptionsItemSelected(MenuItem item){

         if (item.getTitle().equals("Setup")){
            Intent intent = new Intent(this,
com.calculator.setup.class);
            startActivityForResult(intent, MY_SETUP);
            return true;
         }

         if (item.getTitle().equals("About")){
         Intent intent = new Intent(this, com.calculator.about.class);
         startActivityForResult(intent, MY_ABOUT);
            return true;
         }
        return false;
    }
}
----------------------------------------------------------
Create your layout files.  I used dialogsetup.xml and
dialogabout.xml.  I then created two class files called setup.java and
about.java

Here's part of the the setup.java file:

package com.calculator;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;

public class setup extends Activity {

         @Override
            public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                        setContentView(R.layout.dialogsetup);
// put additional code in reference to the dialogsetup screen.
}

--------------------------------
You do the same with the about.java file.  The dialogsetup.xml file
and the dialogabout.xml file contains the layout for that particular
option.

One thing that is most important once you have created these files,
and that is to add an activity to your manifest.xml file for each
activity that you created.  This had two activities so the manifest
file has these entries added to it:

<activity android:name="setup"></activity>
<activity android:name="about"></activity>

If you don't add this, then you will get an error and be forced to
close your application.

Thanks again for all who has guided me in solving this problem.

:RB

On Sep 23, 5:40 pm, rb <rbs...@gmail.com> wrote:
> Hi,
>
> I am attempting to create a simple context menu that will show another
> screen and have some functionality.  What is happening is that if I
> select menu, I get the items (context menu) then when I select one of
> them, it does nothing.  What am I missing here?   Once you click on
> the item, should it not execute the class that you have indicated?
>
> Here's my code:
>
> This is in the main application:
> package com.calculator;
>
> import android.app.Activity;
> import android.content.Intent;
> import android.graphics.Color;
> import android.os.Bundle;
> import android.view.View;
> import android.widget.Button;
> import android.widget.TextView;
>
> public class Calculator extends Activity {
>     /** Called when the activity is first created. */
>     @Override
>     public void onCreate(Bundle savedInstanceState) {
>         super.onCreate(savedInstanceState);
>         setContentView(R.layout.main);
>
> ... code of the project ....
>
>     // context menu
>     public boolean onCreateOptionsMenu(android.view.Menu menu) {
>         super.onCreateOptionsMenu(menu);
>         menu.add("Setup")
>                 .setIcon(android.R.drawable.ic_menu_preferences)
>                 .setIntent(new Intent(this, setup.class));
>         menu.add("About")
>                 .setIcon(android.R.drawable.ic_menu_info_details)
>                 .setIntent(new Intent(this, about.class));
>         return true;
>     }
>
> }
>
> --------------------------------------------------
>
> Here's the code for the "setup.java" class.  I have created a layout
> page called "setup.xml".
> All this should do is show me.
>
> package com.calculator;
>
> import android.app.Activity;
> import android.os.Bundle;
> import android.widget.Toast;
>
> public class setup extends Activity {
>
>         �...@override
>             public void onCreate(Bundle savedInstanceState) {
>                 super.onCreate(savedInstanceState);
>                         setContentView(R.layout.setup);
>          }
>
> }
>
> --------------------------------------------------
>
> I have placed the setContentView(R.layout.setup); in the main and used
> different coding for context menus and it did show me the other
> screen, but if I select menu, it gives me the same context menu.  I
> figure that the above code would direct me to the individual class
> where you can code it specifically and show what you want.   Any ideas
> on what I am missing here?   Please forgive me as I am new to this.
> Any complete code would be appreciated.

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to