tags:

views:

782

answers:

1

Hi there,

I would if anyone can help.. I am coming from a c# background so i have the syntax basics of java and i following the hello world tutorial which has 1 activity (inherits from Activity) which basically shows my view like so

     setContentView(R.layout.main);

So what i wanted to do (by viewing a tutorial) is create a menu by implementing (override) this method

     onCreateOptionsMenu

but my activity class inherits Activity and it seems that this is not available in the superclass..

I am a little confused, can i create more than 1 Activity class? hence should i create a Menus Activity class that inherits from a different superclass?

If this is the case how would i call the new activity class i have created from my

     public void onCreate

which is implemented on my main activity class that inherits Activity.

I am a little confused and i can't seem to find any concrete info with regards to this.

Any ideas?

Here is my simple activity class (its basically shows the view)

public class DGAdmin extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    //TextView tv = new TextView(this);
    //tv.setText("Hello, Android");
    //setContentView(tv);

}

}

+3  A: 

For a menu, add (e.g.) the following method to your class DGAdmin:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);

    // Add two example menu buttons
    int groupId = 0;
    int menuItemOrder = Menu.NONE;

    int menuItemId = ITEM_1;
    int menuItemText = "item 1";
    MenuItem menuItem = menu.add(groupId, menuItemId, menuItemOrder, menuItemText);
    menuItem.setIcon(R.drawable.menu_item_1_icon);

    menuItemId = ITEM_2;
    menuItemText = "item 2";
    menuItem = menu.add(groupId, menuItemId, menuItemOrder, menuItemText);
    menuItem.setIcon(R.drawable.menu_item_2_icon);
}

Regarding the question about creating and calling a new activity, I think you need to read a little more about the android fundamentals, e.g. the life-cycle of an activity, the different components (activities, services, xml-based layouts, etc) in order to get a better feeling of how to write your android apps. I'm pretty new to android myself, so I know that it can be quite frustrating before you get the hang of it all. But just keep on reading and writing code and all will come clear pretty soon.

Read through the application fundamentals in the dev guide, check out the answers to this question (and this), and check the API reference for the methods or classes that are new to you.

But to explicitly answer your question, you use an intent to start a new activity:

Intent intent = new Intent(callingActivity.this, NewActivity.class);
startActivity(intent);

Or if you want the new activity to return some result when it has finished executing:

startActivityForResult(intent, requestCode);

Hope this can be of at least some help!


UPDATE TO ANSWER COMMENT:

Are you using the following imports in your code?

import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;

If you do and it still doesn't work, I don't know what the problem might be...

Since Android 1.0, the Activity class includes the method onCreateOptionsMenu(), so it should be available to you. Are you using Eclipse as your development environment? If you don't, I really recommend that you start using it with the android-plugin, it is really smooth! Check out how to install it in the answers to this question.

I hope this solves your problem, otherwise maybe someone more experienced than me can tell you what the problem is.

aspartame
Yes it was thanks! -- Just one question, i did try that with onCreateOptionsMenu but it gives the error "Must overrride or implement a supertype method), i think the reason is that my DGAdmin inherits supertype Activity and this doesn't have onCreateOptionsMenu - so i should change it to inherit to something else?Thanks once again
mark smith
I added a response to your comment in my initial answer, hope this solves your problem!
aspartame
It did .. thanks a lot for your comments
mark smith