SlideShare a Scribd company logo
1 of 14
Menus and Dialogs
CS 240 – Advanced Programming Concepts
MENUS
2
Adding a Menu to an Activity
1. Add menu strings to res/values/strings.xml
resource file(s)
2. Add a menu layout file (New -> Android Resource
File)
– Select ’menu’ as the resource type
– Add item attributes
– Example:
ActivityMenuExample/res/menu/main_menu.xml
3. Override onCreateOptionsMenu(Menu)
4. Override onOptionsItemSelected(MenuItem)
• Example: ActivityMenuExample.zip 3
Menu Item Flags
4
https://developer.android.com/guide/topics/resources/menu-resource
Adding an Icon to a Menu
• From the layout file
– android:icon="@drawable/ic_file”
– Use Android Asset Studio to generate the icon
(Android book pgs. 253-255)
– Can also download and add icons
(https://developer.android.com/studio/write/image-
asset-studio)
• From onCreateOptionsMenu(…)
– Get reference to the menu item and write code to add
the icon (required to use FontAwesome icons)
5
Adding a Font Awesome Menu Icon
• Add dependencies to your build.gradle file:
– implementation 'com.joanzapata.iconify:android-
iconify:2.2.2'
– implementation 'com.joanzapata.iconify:android-iconify-
fontawesome:2.2.2'
• If the above dependencies create Gradle build errors,
by bringing in incompatible transitive dependencies
– 'implementation (com.joanzapata.iconify:android-
iconify:2.2.2',
{ exclude group: 'com.android.support' })
– implementation ('com.joanzapata.iconify:android-iconify-
fontawesome:2.2.2',
{ exclude group: 'com.android.support' })
6
Adding a Font Awesome Menu Icon
protected void onCreate(…) {
…
Iconify.with(new FontAwesomeModule());
…
}
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_menu, menu);
MenuItem personMenuItem = menu.findItem(R.id.personMenuItem);
personMenuItem.setIcon(new IconDrawable(this,
FontAwesomeIcons.fa_user)
.colorRes(R.color.colorWhite)
.actionBarSize());
return true;
}
7
Adding a Menu to a Fragment
• Same as Actvity menus with the following
changes:
– Extra Step: Call setHasOptionsMenu(true) in
onCreate(Bundle) or your menu callbacks will never
be called
– Method signature for onCreateOptionsMenu(…)
receives a MenuInflater as a parameter
• Receives the MenuInflater from the hosting activity
• Example: CriminalIntent-Chapt13.zip
– res/menu/fragment_crime_list.xml
– CrimeListFragment.java
8
Implementing an Up Button
• Add an android:parentActivityName
attribute to the manifest entry for the activity
that should have an Up button
• Family Map Client Example (in
AndroidManifest.xml):
<activity
android:name=".view.PersonActivity"
android:parentActivityName=".view.MainActivity" />
9
Implementing an Up Button: Using the
Same Fragment or Activity Instance
• Simply adding a parent activity will cause the
parent activity/fragment to be re-created
• To re-use the same instance (and therefore keep
it’s state), do the following
1. Provide a parent activity (per previous slide)
2. Override onOptionsItemSelected(MenuItem)
3. If ‘up’ button selected:
1. Create an Intent
2. Set Intent.FLAG_ACTIVITY_SINGLE_TOP and
Intent.FLAG_ACTIVITY_CLEAR_TOP flags
3. Start the parent activity from the intent
10
Implementing an Up Button: Using the
Same Fragment or Activity Instance
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId() == android.R.id.home) {
Intent intent = new Intent(this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP |
Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
return true;
}
11
DIALOGS
12
Creating a Simple Dialog
• Create an instance of AlertDialog
– Use AlertDialog.Builder(Context)
• Call show(FragmentManager, String) to make it visible
– Second param = optional tag that can be used to retrieve the fragment
later from the FragmentManager with findFragmentByTag(String)
• Recommended: Wrap in a DialogFragment to keep it from
disappearing on rotation
• Useful AlertDialog.Builder methods:
– setTitle(…)
– setMessage(…)
– setView(…)
– setPositiveButton(…)
– setNegativeButton(…)
– setNeutralButton(…)
– Many others
• Example: SimpleDialogExample.java
13
A More Sophisticated Example
• Android book chapter 12
– CriminalIntent-Chapt13.zip
14

More Related Content

Similar to MenusAnddailogsinandroidddfdadusjsjdjdjs

Desbravando Web Components
Desbravando Web ComponentsDesbravando Web Components
Desbravando Web ComponentsMateus Ortiz
 
Day 4: Android: UI Widgets
Day 4: Android: UI WidgetsDay 4: Android: UI Widgets
Day 4: Android: UI WidgetsAhsanul Karim
 
Day 5: Android User Interface [View Widgets]
Day 5: Android User Interface [View Widgets]Day 5: Android User Interface [View Widgets]
Day 5: Android User Interface [View Widgets]Ahsanul Karim
 
iOS Contact List Application Tutorial
iOS Contact List Application TutorialiOS Contact List Application Tutorial
iOS Contact List Application TutorialIshara Amarasekera
 
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)Bruno Delb
 
11.11.2020 - Unit 5-3 ACTIVITY, MENU AND SQLITE DATABASE.pptx
11.11.2020 - Unit 5-3  ACTIVITY, MENU AND SQLITE DATABASE.pptx11.11.2020 - Unit 5-3  ACTIVITY, MENU AND SQLITE DATABASE.pptx
11.11.2020 - Unit 5-3 ACTIVITY, MENU AND SQLITE DATABASE.pptxMugiiiReee
 
Angular JS2 Training Session #2
Angular JS2 Training Session #2Angular JS2 Training Session #2
Angular JS2 Training Session #2Paras Mendiratta
 
Android Development Training for Beginners - Activity
Android Development Training for Beginners - ActivityAndroid Development Training for Beginners - Activity
Android Development Training for Beginners - ActivityJoemarie Amparo
 
Short Intro to Android Fragments
Short Intro to Android FragmentsShort Intro to Android Fragments
Short Intro to Android FragmentsJussi Pohjolainen
 
B2. activity and intent
B2. activity and intentB2. activity and intent
B2. activity and intentPERKYTORIALS
 
Leture5 exercise onactivities
Leture5 exercise onactivitiesLeture5 exercise onactivities
Leture5 exercise onactivitiesmaamir farooq
 
Lecture exercise on activities
Lecture exercise on activitiesLecture exercise on activities
Lecture exercise on activitiesmaamir farooq
 
Android intents-3 www.j2program.blogspot.com
Android intents-3 www.j2program.blogspot.comAndroid intents-3 www.j2program.blogspot.com
Android intents-3 www.j2program.blogspot.comMohamed Rimzan
 

Similar to MenusAnddailogsinandroidddfdadusjsjdjdjs (20)

Lab1-android
Lab1-androidLab1-android
Lab1-android
 
Desbravando Web Components
Desbravando Web ComponentsDesbravando Web Components
Desbravando Web Components
 
Day 4: Android: UI Widgets
Day 4: Android: UI WidgetsDay 4: Android: UI Widgets
Day 4: Android: UI Widgets
 
Day 5: Android User Interface [View Widgets]
Day 5: Android User Interface [View Widgets]Day 5: Android User Interface [View Widgets]
Day 5: Android User Interface [View Widgets]
 
iOS Contact List Application Tutorial
iOS Contact List Application TutorialiOS Contact List Application Tutorial
iOS Contact List Application Tutorial
 
Eclipse Tricks
Eclipse TricksEclipse Tricks
Eclipse Tricks
 
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 Basic Components
Android Basic ComponentsAndroid Basic Components
Android Basic Components
 
Android action bar and notifications-chapter16
Android action bar and notifications-chapter16Android action bar and notifications-chapter16
Android action bar and notifications-chapter16
 
Android ui menu
Android ui menuAndroid ui menu
Android ui menu
 
11.11.2020 - Unit 5-3 ACTIVITY, MENU AND SQLITE DATABASE.pptx
11.11.2020 - Unit 5-3  ACTIVITY, MENU AND SQLITE DATABASE.pptx11.11.2020 - Unit 5-3  ACTIVITY, MENU AND SQLITE DATABASE.pptx
11.11.2020 - Unit 5-3 ACTIVITY, MENU AND SQLITE DATABASE.pptx
 
Angular JS2 Training Session #2
Angular JS2 Training Session #2Angular JS2 Training Session #2
Angular JS2 Training Session #2
 
Android Development Training for Beginners - Activity
Android Development Training for Beginners - ActivityAndroid Development Training for Beginners - Activity
Android Development Training for Beginners - Activity
 
Short Intro to Android Fragments
Short Intro to Android FragmentsShort Intro to Android Fragments
Short Intro to Android Fragments
 
B2. activity and intent
B2. activity and intentB2. activity and intent
B2. activity and intent
 
Android UI
Android UIAndroid UI
Android UI
 
Leture5 exercise onactivities
Leture5 exercise onactivitiesLeture5 exercise onactivities
Leture5 exercise onactivities
 
Lecture exercise on activities
Lecture exercise on activitiesLecture exercise on activities
Lecture exercise on activities
 
Android intents-3 www.j2program.blogspot.com
Android intents-3 www.j2program.blogspot.comAndroid intents-3 www.j2program.blogspot.com
Android intents-3 www.j2program.blogspot.com
 
Android App development III
Android App development IIIAndroid App development III
Android App development III
 

Recently uploaded

UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingrknatarajan
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...Call Girls in Nagpur High Profile
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...ranjana rawat
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escortsranjana rawat
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escortsranjana rawat
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130Suhani Kapoor
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Christo Ananth
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingrakeshbaidya232001
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSSIVASHANKAR N
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college projectTonystark477637
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Call Girls in Nagpur High Profile
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSKurinjimalarL3
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxupamatechverse
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxpurnimasatapathy1234
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordAsst.prof M.Gokilavani
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxpranjaldaimarysona
 

Recently uploaded (20)

UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
 
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writing
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college project
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptx
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptx
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptx
 

MenusAnddailogsinandroidddfdadusjsjdjdjs

  • 1. Menus and Dialogs CS 240 – Advanced Programming Concepts
  • 3. Adding a Menu to an Activity 1. Add menu strings to res/values/strings.xml resource file(s) 2. Add a menu layout file (New -> Android Resource File) – Select ’menu’ as the resource type – Add item attributes – Example: ActivityMenuExample/res/menu/main_menu.xml 3. Override onCreateOptionsMenu(Menu) 4. Override onOptionsItemSelected(MenuItem) • Example: ActivityMenuExample.zip 3
  • 5. Adding an Icon to a Menu • From the layout file – android:icon="@drawable/ic_file” – Use Android Asset Studio to generate the icon (Android book pgs. 253-255) – Can also download and add icons (https://developer.android.com/studio/write/image- asset-studio) • From onCreateOptionsMenu(…) – Get reference to the menu item and write code to add the icon (required to use FontAwesome icons) 5
  • 6. Adding a Font Awesome Menu Icon • Add dependencies to your build.gradle file: – implementation 'com.joanzapata.iconify:android- iconify:2.2.2' – implementation 'com.joanzapata.iconify:android-iconify- fontawesome:2.2.2' • If the above dependencies create Gradle build errors, by bringing in incompatible transitive dependencies – 'implementation (com.joanzapata.iconify:android- iconify:2.2.2', { exclude group: 'com.android.support' }) – implementation ('com.joanzapata.iconify:android-iconify- fontawesome:2.2.2', { exclude group: 'com.android.support' }) 6
  • 7. Adding a Font Awesome Menu Icon protected void onCreate(…) { … Iconify.with(new FontAwesomeModule()); … } public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.main_menu, menu); MenuItem personMenuItem = menu.findItem(R.id.personMenuItem); personMenuItem.setIcon(new IconDrawable(this, FontAwesomeIcons.fa_user) .colorRes(R.color.colorWhite) .actionBarSize()); return true; } 7
  • 8. Adding a Menu to a Fragment • Same as Actvity menus with the following changes: – Extra Step: Call setHasOptionsMenu(true) in onCreate(Bundle) or your menu callbacks will never be called – Method signature for onCreateOptionsMenu(…) receives a MenuInflater as a parameter • Receives the MenuInflater from the hosting activity • Example: CriminalIntent-Chapt13.zip – res/menu/fragment_crime_list.xml – CrimeListFragment.java 8
  • 9. Implementing an Up Button • Add an android:parentActivityName attribute to the manifest entry for the activity that should have an Up button • Family Map Client Example (in AndroidManifest.xml): <activity android:name=".view.PersonActivity" android:parentActivityName=".view.MainActivity" /> 9
  • 10. Implementing an Up Button: Using the Same Fragment or Activity Instance • Simply adding a parent activity will cause the parent activity/fragment to be re-created • To re-use the same instance (and therefore keep it’s state), do the following 1. Provide a parent activity (per previous slide) 2. Override onOptionsItemSelected(MenuItem) 3. If ‘up’ button selected: 1. Create an Intent 2. Set Intent.FLAG_ACTIVITY_SINGLE_TOP and Intent.FLAG_ACTIVITY_CLEAR_TOP flags 3. Start the parent activity from the intent 10
  • 11. Implementing an Up Button: Using the Same Fragment or Activity Instance @Override public boolean onOptionsItemSelected(MenuItem item) { if(item.getItemId() == android.R.id.home) { Intent intent = new Intent(this, MainActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(intent); } return true; } 11
  • 13. Creating a Simple Dialog • Create an instance of AlertDialog – Use AlertDialog.Builder(Context) • Call show(FragmentManager, String) to make it visible – Second param = optional tag that can be used to retrieve the fragment later from the FragmentManager with findFragmentByTag(String) • Recommended: Wrap in a DialogFragment to keep it from disappearing on rotation • Useful AlertDialog.Builder methods: – setTitle(…) – setMessage(…) – setView(…) – setPositiveButton(…) – setNegativeButton(…) – setNeutralButton(…) – Many others • Example: SimpleDialogExample.java 13
  • 14. A More Sophisticated Example • Android book chapter 12 – CriminalIntent-Chapt13.zip 14