SlideShare a Scribd company logo
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

More Related Content

PPTX
Microsoft Security Incident Report
PDF
Report on Rogue Security Software
PDF
Eclipse rcp 정리(enabled when, contextmenu)
PDF
Create code confidence for better application security
PPT
Remedie OSDC.TW
PPTX
Team Learning Test
PDF
Team Learning and Knowledge Creation PhD research presentation June 2013
PPTX
Symantec Report On Rogue Security Software
Microsoft Security Incident Report
Report on Rogue Security Software
Eclipse rcp 정리(enabled when, contextmenu)
Create code confidence for better application security
Remedie OSDC.TW
Team Learning Test
Team Learning and Knowledge Creation PhD research presentation June 2013
Symantec Report On Rogue Security Software

Viewers also liked (16)

PDF
ISTR XV
PDF
Borgman - Privacy, Policy and Data Governance in the University
ODP
Twitter terms of service and privacy policy analysis
PPTX
Binomial heap (a concept of Data Structure)
PPT
android menus
PPTX
Linear programming production strategy body plus 100, 200
PDF
Team learning in the upper secondary school - focus changes from teaching to ...
PPTX
Create Agile confidence for better application security
PDF
IBM Security Software Solutions
PPTX
Fibonacci Heaps
PPTX
Navigating agile automotive software development
PDF
Agile Software Development - Learn It Yourself
PPTX
Binomial Heaps and Fibonacci Heaps
PDF
Лекция 6. Фибоначчиевы кучи (Fibonacci heaps)
PDF
Fibonacci Heap
PPTX
Binomial heap presentation
ISTR XV
Borgman - Privacy, Policy and Data Governance in the University
Twitter terms of service and privacy policy analysis
Binomial heap (a concept of Data Structure)
android menus
Linear programming production strategy body plus 100, 200
Team learning in the upper secondary school - focus changes from teaching to ...
Create Agile confidence for better application security
IBM Security Software Solutions
Fibonacci Heaps
Navigating agile automotive software development
Agile Software Development - Learn It Yourself
Binomial Heaps and Fibonacci Heaps
Лекция 6. Фибоначчиевы кучи (Fibonacci heaps)
Fibonacci Heap
Binomial heap presentation
Ad

Similar to Android Lab Test : Creating a menu context (english) (6)

PPTX
Android Lab Test : Creating a menu dynamically (english)
DOCX
Android menus in android-chapter15
PDF
Android ui menu
PPTX
MAD_MENU286nvhvchvhmvjvjvmbvmbvmvbbm.pptx
PDF
Lecture-10-Menus.pdf of Mobile Application Development
PDF
01 10 - graphical user interface - others
Android Lab Test : Creating a menu dynamically (english)
Android menus in android-chapter15
Android ui menu
MAD_MENU286nvhvchvhmvjvjvmbvmbvmvbbm.pptx
Lecture-10-Menus.pdf of Mobile Application Development
01 10 - graphical user interface - others
Ad

More from Bruno Delb (20)

PPTX
Introduction to Swift (tutorial)
PPTX
Android Lab Test : Storage of data with SharedPreferences (english)
PPTX
Android Lab Test : Using the sensor gyroscope (english)
PPTX
Android Lab Test : Using the network with HTTP (english)
PPTX
Android Lab Test : Managing sounds with SoundPool (english)
PPTX
Android Lab Test : Using the text-to-speech (english)
PPTX
Android Lab Test : Reading the foot file list (english)
PPTX
Android Lab Test : Creating a dialog Yes/No (english)
PPTX
Android Lab Test : The styles of views (english)
PPTX
Android Lab Test : Using the camera preview (english)
PPTX
Android Lab Test : The views, the Gallery (english)
PPTX
Android Lab Test : Using the WIFI (english)
PPTX
Android Lab Test : Managing the telephone calls (english)
PPTX
Android Lab Test : Reading the SMS-inbox (english)
PPTX
Android Lab Test : Installation of application in Java (english)
PPTX
Android Lab Test : Ecrire un texte sur le canevas (français)
PPTX
Android Lab Test : La connectivité réseau avec HTTP (français)
PPTX
Android Lab Test : Le capteur gyroscope (français)
PPTX
Android Lab Test : Les threads (français)
PPTX
Android Lab Test : L'installation d'une application en Java (français)
Introduction to Swift (tutorial)
Android Lab Test : Storage of data with SharedPreferences (english)
Android Lab Test : Using the sensor gyroscope (english)
Android Lab Test : Using the network with HTTP (english)
Android Lab Test : Managing sounds with SoundPool (english)
Android Lab Test : Using the text-to-speech (english)
Android Lab Test : Reading the foot file list (english)
Android Lab Test : Creating a dialog Yes/No (english)
Android Lab Test : The styles of views (english)
Android Lab Test : Using the camera preview (english)
Android Lab Test : The views, the Gallery (english)
Android Lab Test : Using the WIFI (english)
Android Lab Test : Managing the telephone calls (english)
Android Lab Test : Reading the SMS-inbox (english)
Android Lab Test : Installation of application in Java (english)
Android Lab Test : Ecrire un texte sur le canevas (français)
Android Lab Test : La connectivité réseau avec HTTP (français)
Android Lab Test : Le capteur gyroscope (français)
Android Lab Test : Les threads (français)
Android Lab Test : L'installation d'une application en Java (français)

Recently uploaded (20)

PDF
Chinmaya Tiranga quiz Grand Finale.pdf
PPTX
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
PDF
Computing-Curriculum for Schools in Ghana
PDF
Complications of Minimal Access Surgery at WLH
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
01-Introduction-to-Information-Management.pdf
PDF
O7-L3 Supply Chain Operations - ICLT Program
PPTX
202450812 BayCHI UCSC-SV 20250812 v17.pptx
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PPTX
Lesson notes of climatology university.
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PPTX
Institutional Correction lecture only . . .
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Chinmaya Tiranga quiz Grand Finale.pdf
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
Computing-Curriculum for Schools in Ghana
Complications of Minimal Access Surgery at WLH
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
VCE English Exam - Section C Student Revision Booklet
01-Introduction-to-Information-Management.pdf
O7-L3 Supply Chain Operations - ICLT Program
202450812 BayCHI UCSC-SV 20250812 v17.pptx
STATICS OF THE RIGID BODIES Hibbelers.pdf
Lesson notes of climatology university.
Final Presentation General Medicine 03-08-2024.pptx
Supply Chain Operations Speaking Notes -ICLT Program
Institutional Correction lecture only . . .
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
102 student loan defaulters named and shamed – Is someone you know on the list?
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx

Android Lab Test : Creating a menu context (english)

  • 1. 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
  • 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 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"); }
  • 9. 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); } }
  • 10. Test on your mobile View_Menu_Context
  • 11. 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