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 : The contextual menu
The contextual menu
• In this lesson, you will learn to add a
contextual menu to a view.
• For this, you will use the methods of the
ContextMenu.
The contextual menu
• In first, the contextual menu shoud be attached to an object, for
example to a button :
registerForContextMenu(btnMenu);
The contextual menu
• The contextual menu should be created with the method
onCreateContextMenu().
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
getMenuInflater().inflate(R.menu.contextmenu, menu);
menu.setHeaderTitle("Choose an option");
}
The contextual menu
• To each selection of an option of the contextual menu,
the method onContextItemSelected() is called with in his
parameter the selected item (MenuItem).
public boolean onContextItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.option1: return true;
case R.id.option2: return true;
case R.id.option3: return true;
}
return super.onContextItemSelected(item);
}
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">
<Button
android:id="@+id/btnMenu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Menu" />
</LinearLayout>
Menu resmenucontextmenu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/option1"
android:title="Option 1" />
<item
android:id="@+id/option2"
android:title="Option 2" />
<item
android:id="@+id/option3"
android:title="Option 3" />
</menu>
File Main.java
public class Main extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btnMenu = (Button)findViewById (R.id.btnMenu);
btnMenu.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
openContextMenu(v);
}
});
registerForContextMenu(btnMenu);
}
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo
menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
getMenuInflater().inflate(R.menu.contextmenu, menu);
menu.setHeaderTitle("Choose an option");
}
File Main.java
public boolean onContextItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.option1:
Toast.makeText(this,"Option 1",1000).show();
return true;
case R.id.option2:
Toast.makeText(this,"Option 2",1000).show();
return true;
case R.id.option3:
Toast.makeText(this,"Option 3",1000).show();
return true;
}
return super.onContextItemSelected(item);
}
}
Test on your mobile
View_Menu_Context
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 context (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 : The contextual menu
  • 2.
    The contextual menu •In this lesson, you will learn to add a contextual menu to a view. • For this, you will use the methods of the ContextMenu.
  • 3.
    The contextual menu •In first, the contextual menu shoud be attached to an object, for example to a button : registerForContextMenu(btnMenu);
  • 4.
    The contextual menu •The contextual menu should be created with the method onCreateContextMenu(). public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { super.onCreateContextMenu(menu, v, menuInfo); getMenuInflater().inflate(R.menu.contextmenu, menu); menu.setHeaderTitle("Choose an option"); }
  • 5.
    The contextual menu •To each selection of an option of the contextual menu, the method onContextItemSelected() is called with in his parameter the selected item (MenuItem). public boolean onContextItemSelected(MenuItem item) { switch(item.getItemId()) { case R.id.option1: return true; case R.id.option2: return true; case R.id.option3: return true; } return super.onContextItemSelected(item); }
  • 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"> <Button android:id="@+id/btnMenu" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Menu" /> </LinearLayout>
  • 7.
    Menu resmenucontextmenu.xml <?xml version="1.0"encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/option1" android:title="Option 1" /> <item android:id="@+id/option2" android:title="Option 2" /> <item android:id="@+id/option3" android:title="Option 3" /> </menu>
  • 8.
    File Main.java public classMain extends Activity { protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button btnMenu = (Button)findViewById (R.id.btnMenu); btnMenu.setOnClickListener(new OnClickListener() { public void onClick(View v) { openContextMenu(v); } }); registerForContextMenu(btnMenu); } public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { super.onCreateContextMenu(menu, v, menuInfo); getMenuInflater().inflate(R.menu.contextmenu, menu); menu.setHeaderTitle("Choose an option"); }
  • 9.
    File Main.java public booleanonContextItemSelected(MenuItem item) { switch(item.getItemId()) { case R.id.option1: Toast.makeText(this,"Option 1",1000).show(); return true; case R.id.option2: Toast.makeText(this,"Option 2",1000).show(); return true; case R.id.option3: Toast.makeText(this,"Option 3",1000).show(); return true; } return super.onContextItemSelected(item); } }
  • 10.
    Test on yourmobile View_Menu_Context
  • 11.
    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