SlideShare ist ein Scribd-Unternehmen logo
1 von 11
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

Weitere ähnliche Inhalte

Andere mochten auch

Linear programming production strategy body plus 100, 200
Linear programming   production strategy body plus 100, 200Linear programming   production strategy body plus 100, 200
Linear programming production strategy body plus 100, 200
Prabhat Taneja
 
Binomial heap presentation
Binomial heap presentationBinomial heap presentation
Binomial heap presentation
Hafsa.Naseem
 

Andere mochten auch (16)

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

Ähnlich wie Android Lab Test : Creating a menu context (english) (8)

Android Lab Test : Creating a menu dynamically (english)
Android Lab Test : Creating a menu dynamically (english)Android Lab Test : Creating a menu dynamically (english)
Android Lab Test : Creating a menu dynamically (english)
 
Android session 3
Android session 3Android session 3
Android session 3
 
Intake 37 8
Intake 37 8Intake 37 8
Intake 37 8
 
Intake 38 8
Intake 38 8Intake 38 8
Intake 38 8
 
Android ui menu
Android ui menuAndroid ui menu
Android ui menu
 
Menu bars and menus
Menu bars and menusMenu bars and menus
Menu bars and menus
 
Drafting Board self-guided training
Drafting Board self-guided trainingDrafting Board self-guided training
Drafting Board self-guided training
 
Lecture #3 activities and intents
Lecture #3  activities and intentsLecture #3  activities and intents
Lecture #3 activities and intents
 

Mehr von Bruno Delb

Mehr von Bruno Delb (20)

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

Kürzlich hochgeladen

The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
ZurliaSoop
 

Kürzlich hochgeladen (20)

General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 

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