Develop on Android
Android Lab Test
www.AndroidLabTest.com
Youku
By Bruno Delb
www.weibo.com/brunodelb
i.youku.com/brunodelb | www.weibo.com/brunodelb | blog.delb.cn
http://i.youku.com/brunoparis
Weibo
Officialsite
Lesson : Menu created dynamically
Menu created dynamically
• In this lesson, you will learn to create a menu
dynamically.
• For this, you will use MenuItem and
onOptionsItemSelected.
Menu created dynamically
• To create the menu, use the method onCreateOptionsMenu().
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
...
return true;
}
• To select the keyboard mode (for example if you use
shortcuts), use the method setQwertyMode() :
menu.setQwertyMode(true);
Menu created dynamically
• To add an item to the menu, use the method add() :
MenuItem menuItem2 = menu.add(0, 1, 1, "Item 2");
• To add a shortcut on an item of the menu, use the method
setAlphabeticShortcut() :
menuItem2.setAlphabeticShortcut('b');
• To add an icon to an item of the menu, use the methode setIcon.
menuItem2.setIcon (R.drawable.app);
Menu created dynamically
• The method onOptionsItemSelected() is called at each selection of
an item of the menu with as argument the selected item MenuItem.
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case 0: // Item 1
return true;
case 1: // Item 2
return true;
}
return false;
}
Layout main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</LinearLayout>
File Main.java
public class Main extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
menu.setQwertyMode(true);
MenuItem menuItem1 = menu.add(0, 0, 0, "Item 1");
menuItem1.setAlphabeticShortcut('a');
MenuItem menuItem2 = menu.add(0, 1, 1, "Item 2");
menuItem2.setAlphabeticShortcut('b');
menuItem2.setIcon (R.drawable.app);
MenuItem menuItem3 = menu.add(0, 2, 2, "Item 3");
menuItem3.setAlphabeticShortcut('c');
return true;
}
File Main.java
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case 0:
Toast.makeText(this, "Item 1", Toast.LENGTH_LONG).show();
return true;
case 1:
Toast.makeText(this, "Item 2", Toast.LENGTH_LONG).show();
return true;
case 2:
Toast.makeText(this, "Item 3", Toast.LENGTH_LONG).show();
return true;
}
return false;
}
}
Test on your mobile
View_Menu_Dynamic
Follow me on my channel PengYooTV …
On my Youku channel
http://i.youku.com/brunoparis
Who am I ?
Bruno Delb (www.delb.cn),
Author of the first french book of development of Java mobile application (2002),
Consultant, project manager and developer of social & mobile applications,
let’s talk about your needs ...
And on Weibo :
http://www.weibo.com/brunodelb

Android Lab Test : Creating a menu dynamically (english)

  • 1.
    Develop on Android AndroidLab Test www.AndroidLabTest.com Youku By Bruno Delb www.weibo.com/brunodelb i.youku.com/brunodelb | www.weibo.com/brunodelb | blog.delb.cn http://i.youku.com/brunoparis Weibo Officialsite Lesson : Menu created dynamically
  • 2.
    Menu created dynamically •In this lesson, you will learn to create a menu dynamically. • For this, you will use MenuItem and onOptionsItemSelected.
  • 3.
    Menu created dynamically •To create the menu, use the method onCreateOptionsMenu(). public boolean onCreateOptionsMenu(Menu menu) { super.onCreateOptionsMenu(menu); ... return true; } • To select the keyboard mode (for example if you use shortcuts), use the method setQwertyMode() : menu.setQwertyMode(true);
  • 4.
    Menu created dynamically •To add an item to the menu, use the method add() : MenuItem menuItem2 = menu.add(0, 1, 1, "Item 2"); • To add a shortcut on an item of the menu, use the method setAlphabeticShortcut() : menuItem2.setAlphabeticShortcut('b'); • To add an icon to an item of the menu, use the methode setIcon. menuItem2.setIcon (R.drawable.app);
  • 5.
    Menu created dynamically •The method onOptionsItemSelected() is called at each selection of an item of the menu with as argument the selected item MenuItem. public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case 0: // Item 1 return true; case 1: // Item 2 return true; } return false; }
  • 6.
    Layout main.xml <?xml version="1.0"encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> </LinearLayout>
  • 7.
    File Main.java public classMain extends Activity { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } public boolean onCreateOptionsMenu(Menu menu) { super.onCreateOptionsMenu(menu); menu.setQwertyMode(true); MenuItem menuItem1 = menu.add(0, 0, 0, "Item 1"); menuItem1.setAlphabeticShortcut('a'); MenuItem menuItem2 = menu.add(0, 1, 1, "Item 2"); menuItem2.setAlphabeticShortcut('b'); menuItem2.setIcon (R.drawable.app); MenuItem menuItem3 = menu.add(0, 2, 2, "Item 3"); menuItem3.setAlphabeticShortcut('c'); return true; }
  • 8.
    File Main.java public booleanonOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case 0: Toast.makeText(this, "Item 1", Toast.LENGTH_LONG).show(); return true; case 1: Toast.makeText(this, "Item 2", Toast.LENGTH_LONG).show(); return true; case 2: Toast.makeText(this, "Item 3", Toast.LENGTH_LONG).show(); return true; } return false; } }
  • 9.
    Test on yourmobile View_Menu_Dynamic
  • 10.
    Follow me onmy channel PengYooTV … On my Youku channel http://i.youku.com/brunoparis Who am I ? Bruno Delb (www.delb.cn), Author of the first french book of development of Java mobile application (2002), Consultant, project manager and developer of social & mobile applications, let’s talk about your needs ... And on Weibo : http://www.weibo.com/brunodelb