Creating A Menu

Put your problem here if it does not fit any of the other categories.

Creating A Menu

Postby joshmmo » Mon Mar 22, 2010 9:08 am

Alright so fyi im starting my very first app so please any help/info is GREATLY appreciated! (and my java is limited so if you wouldn't mind breaking down your replys so I can understand/learn that would be sweet.

Ive also tried to search for this already I found some things but I still have questions.

My goal is to create a menu that links to a Preference Activity like this:

http://android-journey.blogspot.com/201 ... ed-to.html

I think I pretty much have my Preference activity setup so Im not too worry about that but I want to link it to my main activity with the menu.

Ok so my problem is I looked up in the dev android docs and found out that I need an Options Menu.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. /* Creates the menu items */
  3.  
  4.  public boolean onCreateOptionsMenu(Menu menu) {
  5.  
  6.     menu.add(0, MENU_SETTINGS, 0, "Settings");
  7.  
  8.     menu.add(0, MENU_QUIT, 0, "Quit");
  9.  
  10.     return true;
  11.  
  12.  }
  13.  
  14.  
  15.  
  16.     /* Handles item selections */
  17.  
  18.   public boolean onOptionsItemSelected(MenuItem item) {
  19.  
  20.      switch (item.getItemId()) {
  21.  
  22.      case MENU_SETTINGS:    
  23.  
  24.      settings();
  25.  
  26.      return true;
  27.  
  28.   case MENU_QUIT:
  29.  
  30.       quit();
  31.  
  32.       return true;
  33.  
  34.    }
  35.  
  36.    return false;
  37.  
  38. }
  39.  
  40.  
Parsed in 0.033 seconds, using GeSHi 1.0.8.4


Im just not quite sure what this means. Of course when I copy and pasted this from the dev docs, eclipse gives errors for MENU_SETTINGS and MENU_QUIT. I need to define them somehow?

When someone clicks Settings I want it to goto the Preference activity, which I need an intent?? Right? I just dont know how to do it.

Once again THANKYOU so much for your help.
joshmmo
Freshman
Freshman
 
Posts: 6
Joined: Sun Mar 21, 2010 9:59 pm

Top

Postby asolanki » Mon Mar 22, 2010 10:31 am

Hi!!!
public boolean onCreateOptionsMenu(Menu menu)

this function is used to create menu

public boolean onOptionsItemSelected(MenuItem item

this function is called when user select any option item..

MENU_SETTINGS & MENU_QUIT is just static int.

you can define it like
private static final int MENU_SETTINGS = Menu.FIRST;
private static final int MENU_QUIT = Menu.FIRST + 1;

and to start new activity you need to create intent

Intent i = new Intent();
i.setClassName("package name","class name");
startActivity(i);
asolanki
Senior Developer
Senior Developer
 
Posts: 151
Joined: Thu Jan 08, 2009 7:32 am

Postby joshmmo » Mon Mar 22, 2010 6:44 pm

Thankyou for your reply.

Another question,

Now this im sure is just because of my lack of knowledge in java. I need this broken down so I can understand please:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. private static final int MENU_SETTINGS = Menu.FIRST;
  3.  
  4. private static final int MENU_QUIT = Menu.FIRST + 1;
  5.  
  6.  
Parsed in 0.032 seconds, using GeSHi 1.0.8.4


So this will create an integer called MENU_SETTINGS and MENU_QUIT right but what does Menu.FIRST and Menu.FIRST + 1 do exactly? I dont understand what im defining them as, and why i need to do it.


Now for my intent this works for an activity within my own application right? so id want something like this:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. public boolean onOptionsItemSelected(MenuItem item) {
  3.  
  4.         switch (item.getItemId()) {
  5.  
  6.         case MENU_SETTINGS:
  7.  
  8.                 Intent i = new Intent();
  9.  
  10.                 i.setClassName("Easy Music Sync","class name");
  11.  
  12.                 startActivity(i);
  13.  
  14.             return true;
  15.  
  16.  
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


class name = class name of my preference activity? Can you call stuff in other activities like that? Cause eclipse just gives me errors.
joshmmo
Freshman
Freshman
 
Posts: 6
Joined: Sun Mar 21, 2010 9:59 pm

Postby asolanki » Tue Mar 23, 2010 5:40 am

Hi!!!

see Menu.FIRST is a predefined constant.
to add more option in Menu group we have to give specific int value to them .
so we give them value according to that constant..
so afterward we can identify them.

Second thing about starting activity

i dont have much knowledge about preference activity but we can start any activity by intent

in the intent we have to pass class name of our activity which we want to start

like this if i have "myActivity extends Activity" class in com.android package then i will start this activity by


Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. Intent i = new Intent();
  3.  
  4. i.setClassName("com.android","com.android.myActivity");
  5.  
  6. startActivity(i);
  7.  
  8.  
Parsed in 0.035 seconds, using GeSHi 1.0.8.4
asolanki
Senior Developer
Senior Developer
 
Posts: 151
Joined: Thu Jan 08, 2009 7:32 am

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 12 guests