Android Developer Fundamentals V2 - Menus and Dialogs PDF

Summary

This document details menus and dialogs in Android development. It covers various types of menus, such as app bars and contextual menus. It also addresses dialogs, including alert, date, and time pickers. The content includes code examples.

Full Transcript

Android Developer Fundamentals V2 Menu This work is licensed under a Creative Menus and Android Developer Fundamentals V2 Menus and pickers Co...

Android Developer Fundamentals V2 Menu This work is licensed under a Creative Menus and Android Developer Fundamentals V2 Menus and pickers Commons Attribution 4.0 International 1 pickers License. Contents Overview App Bar with Options Menu Popup menus Dialogs This work is licensed under a Creative Menus and Android Developer Fundamentals V2 Commons Attribution 4.0 International 2 pickers License. Overview Android Developer Fundamentals V2 3 Types of Menus 1. App bar with options menu 2. Context menu 3. Contextual action bar 4. Popup menu This work is licensed under a Creative Menus and Android Developer Fundamentals V2 Commons Attribution 4.0 International 4 pickers License. Dialogs and pickers 1. Alert dialog 2. Date picker 3. Time picker 1 2 3 Menus and This work is licensed under a Creative Android Developer Fundamentals V2 Commons Attribution 4.0 International 5 pickers License. App Bar with Options Menu Android Developer Fundamentals V2 6 What is the App Bar? Bar at top of each screen—same for all devices (usually) 1. Nav icon to open navigation drawer 2. Title of current Activity 3. Icons for options menu items 4. Action overflow button for the rest of the options menu This work is licensed under a Creative Menus and Android Developer Fundamentals V2 Commons Attribution 4.0 International 7 pickers License. What is the options menu? Action icons in the app bar for important items (1) Tap the three dots, the "action overflow button" to see the options menu (2) Appears in the right corner of the app bar (3) For navigating to other activities and editing app settings This work is licensed under a Creative Menus and Android Developer Fundamentals V2 Commons Attribution 4.0 International 8 pickers License. Adding Options Menu Android Developer Fundamentals V2 9 Steps to implement options menu 1. XML menu resource (menu_main.xml) 2. onCreateOptionsMenu() to inflate the menu 3. onClick attribute or onOptionsItemSelected() 4. Method to handle item click This work is licensed under a Creative Menus and Android Developer Fundamentals V2 Commons Attribution 4.0 International 10 pickers License. 1. Create menu resource i. Create menu resource directory ii. Create XML menu resource (menu_main.xml) iii. Add entry for each menu item (Settings and Favorites): This work is licensed under a Creative Menus and Android Developer Fundamentals V2 Commons Attribution 4.0 International 11 pickers License. 2. Inflate options menu Override onCreateOptionsMenu() in Activity @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.menu_main, menu); return true; } This work is licensed under a Creative Menus and Android Developer Fundamentals V2 Commons Attribution 4.0 International 12 pickers License. Add icons for menu items i. Right-click drawable ii. Choose New > Image Asset iii. Choose Action Bar and Tab Items iv. Edit the icon name v. Click clipart image, and click icon vi. Click Next, then Finish This work is licensed under a Creative Menus and Android Developer Fundamentals V2 Commons Attribution 4.0 International 13 pickers License. Add menu item attributes This work is licensed under a Creative Menus and Android Developer Fundamentals V2 Commons Attribution 4.0 International 14 pickers License. 3. Override onOptionsItemSelected() @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.action_settings: showSettings(); return true; case R.id.action_favorites: showFavorites(); return true; default: return super.onOptionsItemSelected(item); } } This work is licensed under a Creative Menus and Android Developer Fundamentals V2 Commons Attribution 4.0 International 15 pickers License. Contextual Menus Android Developer Fundamentals V2 16 What are contextual menus? Allows users to perform action on selected View Can be deployed on any View Most often used for items in RecyclerView, GridView, or other View collection This work is licensed under a Creative Menus and Android Developer Fundamentals V2 Commons Attribution 4.0 International 17 pickers License. Types of contextual menus Floating context menu—long-press on a View ○ User can modify View or use it in some fashion ○ User performs action on one View at a time Contextual action mode—temporary action bar in place of or underneath app bar ○ Action items affect the selected View element(s) ○ User can perform action on multiple View elements at once This work is licensed under a Creative Menus and Android Developer Fundamentals V2 Commons Attribution 4.0 International 18 pickers License. Floating Context Menu Android Developer Fundamentals V2 19 Steps 1. Create XML menu resource file and assign appearance and position attributes 2. Register View using registerForContextMenu() 3. Implement onCreateContextMenu() in Activity to inflate menu 4. Implement onContextItemSelected() to handle menu item clicks 5. Create method to perform action for each context menu item This work is licensed under a Creative Menus and Android Developer Fundamentals V2 Commons Attribution 4.0 International 20 pickers License. 1. Create menu resource i. Create XML menu resource (menu_context.xml) This work is licensed under a Creative Menus and Android Developer Fundamentals V2 Commons Attribution 4.0 International 21 pickers License. 2. Register a view to a context menu In onCreate() of the Activity: Register View.OnCreateContextMenuListener to View: TextView article_text = findViewById(R.id.article); registerForContextMenu(article_text); This work is licensed under a Creative Menus and Android Developer Fundamentals V2 Commons Attribution 4.0 International 22 pickers License. 3. Implement onCreateContextMenu() onCreateContextMenu() method Specify which context menu @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { super.onCreateContextMenu(menu, v, menuInfo); MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.menu_context, menu); } This work is licensed under a Creative Menus and Android Developer Fundamentals V2 Commons Attribution 4.0 International 23 pickers License. 4. Implement onContextItemSelected() onCreateContextMenu() @Override method public boolean onContextItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.context_edit: editNote(); return true; case R.id.context_share: shareNote(); return true; default: return super.onContextItemSelected(item); } } This work is licensed under a Creative Menus and Android Developer Fundamentals V2 Commons Attribution 4.0 International 24 pickers License. Dialogs Android Developer Fundamentals V2 25 Dialogs Dialog appears on top, interrupting flow of Activity Requires user action to dismiss TimePickerDialog DatePickerDialog AlertDialog Menus and This work is licensed under a Creative Android Developer Fundamentals V2 Commons Attribution 4.0 International 26 pickers License. AlertDialog AlertDialog can show: 1. Title (optional) 2. Content area 3. Action buttons Menus and This work is licensed under a Creative Android Developer Fundamentals V2 Commons Attribution 4.0 International 27 pickers License. Build the AlertDialog Use AlertDialog.Builder to build alert dialog and set attributes: public void onClickShowAlert(View view) { AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this); alertDialog.setTitle("Connect to Provider"); alertDialog.setMessage(R.string.alert_message); //... Code to set buttons goes here. Menus and This work is licensed under a Creative Android Developer Fundamentals V2 Commons Attribution 4.0 International 28 pickers License. Set the button actions alertDialog.setPositiveButton() alertDialog.setNeutralButton() alertDialog.setNegativeButton() Menus and This work is licensed under a Creative Android Developer Fundamentals V2 Commons Attribution 4.0 International 29 pickers License. alertDialog code example alertDialog.setPositiveButton( "OK", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { // User clicked OK button. } }); Same pattern for setNegativeButton() and setNeutralButton() Menus and This work is licensed under a Creative Android Developer Fundamentals V2 Commons Attribution 4.0 International 30 pickers License. END Android Developer Fundamentals V2 31

Use Quizgecko on...
Browser
Browser