Working with menus

Quickly share your Android Code Snippets here...

Working with menus

Postby JonnySnip3r » Thu Jun 18, 2009 12:40 pm

Hey does anybody know a good place to get more information on menu? when a user selects the menu it displays some choices and maybe pictures.

or if anyone can help me with this it would be great.
JonnySnip3r
Developer
Developer
 
Posts: 30
Joined: Wed Apr 08, 2009 1:23 pm

Top

Postby Mr_Tremor » Thu Jun 18, 2009 7:00 pm

Go through the notepad tutorial on the android developer site. It should be available as a sample app in your SDK so you can just go into the source and pull out the menu code. It has code for Menus when you click the menu button or when you do a long press on an item to show a context menu.
Mr_Tremor
Developer
Developer
 
Posts: 26
Joined: Tue Jun 02, 2009 6:35 pm

Postby JonnySnip3r » Thu Jun 18, 2009 7:55 pm

for those who are wondering how to do this, this is how i have done it.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. @Override
  2.  
  3.     public boolean onCreateOptionsMenu(Menu menu) {
  4.  
  5.         super.onCreateOptionsMenu(menu);
  6.  
  7.         menu.add(0, INSERT_ID, 0, R.string.menu_insert);
  8.  
  9.         return true;
  10.  
  11.     }
  12.  
  13. }
  14.  
  15.  
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


you add the code above in the section you want the menu, and then you need to add the next line to the strings section.

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <string name="menu_insert">About</string>
Parsed in 0.000 seconds, using GeSHi 1.0.8.4
JonnySnip3r
Developer
Developer
 
Posts: 30
Joined: Wed Apr 08, 2009 1:23 pm

Postby JonnySnip3r » Thu Jun 18, 2009 8:18 pm

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. @Override
  2.  
  3.     public boolean onCreateOptionsMenu(Menu menu) {
  4.  
  5.         super.onCreateOptionsMenu(menu);
  6.  
  7.         menu.add(0, INSERT_ID, 0, R.string.menu_insert);
  8.  
  9.         return true;
  10.  
  11.        
  12.  
  13.     }
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


i have this piece of code for the menu, anybody know how to exit the program once the press it?
JonnySnip3r
Developer
Developer
 
Posts: 30
Joined: Wed Apr 08, 2009 1:23 pm

Postby virtual » Fri Jun 19, 2009 6:36 am

Call activity's finish()
Regards,
Virtual
virtual
Developer
Developer
 
Posts: 26
Joined: Tue Mar 24, 2009 11:20 am

Re: Working with menus

Postby dukevalefor » Sun Jun 06, 2010 9:16 pm

Hey Hey,

I´m trying to write my own application but i dont understand where to put the finish exactly...


Here is my code:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. // Public Class
  2. public class LRB extends Activity {
  3.        
  4.  
  5.         WebView webview;
  6.         private static final int MENU_QUIT = 0;
  7.         private static final MenuItem Item = null;
  8.  
  9.        
  10.        
  11.        
  12.        
  13.     /** Called when the activity is first created. */
  14.    
  15. // Public Void    
  16. public void onCreate(Bundle savedInstanceState) {
  17.         super.onCreate(savedInstanceState);
  18.         setContentView(R.layout.main);
  19.    
  20.        
  21.        
  22.        
  23.        
  24. // Launch Webbrowser        
  25.         webview = (WebView) findViewById(R.id.WebView01);
  26.         webview.getSettings().setJavaScriptEnabled(true);
  27.         webview.loadUrl("http://www.google.com");
  28.      
  29. }
  30.      
  31.  
  32. // Menu
  33. public boolean onCreateOptionsMenu (Menu menu) {
  34.         menu.add(0, MENU_QUIT,0, "Close Application");
  35.         return true;
  36.        
  37.                onOptionsItemSelected (MenuItem MENU_QUIT)
  38.                {finish();
  39.         return true;
  40. }
  41.  
  42. }
  43. }
Parsed in 0.037 seconds, using GeSHi 1.0.8.4
dukevalefor
Once Poster
Once Poster
 
Posts: 1
Joined: Sun Jun 06, 2010 9:00 pm

Top

Return to Code Snippets for Android

Who is online

Users browsing this forum: No registered users and 2 guests