Android Using Menus Notes are based on: The Busy Coder's Guide to Android Development by Mark L. Murphy Copyright © 2008-2009 CommonsWare, LLC. ISBN: 978-0-9816780-0-9.

Slides:



Advertisements
Similar presentations
CE881: Mobile and Social Application Programming Simon M. Lucas Menus and Dialogs.
Advertisements

Microsoft Office 2007-Illustrated Introductory, Windows Vista Edition Windows XP Unit A.
Android Using Menus Yuliana Setiowati Rizky Yuniar Hakkun Ahmad Syauqi Ahsan 1Politeknik Elektronika Negeri Surabaya.
Android Fonts Notes are based on: The Busy Coder's Guide to Android Development by Mark L. Murphy Copyright © CommonsWare, LLC. ISBN:
User Interface Classes.  Design Principles  Views & Layouts  Event Handling  Menus  Dialogs.
Application Fundamentals. See: developer.android.com/guide/developing/building/index.html.
Cosc 5/4730 Blackberry and Android: Menus. BLACKBERRY.
User Interface Android Applications. Activities An activity presents a visual user interface. Each activity is given a default window to draw in. The.
Android Form Elements. Views Provide common UI functionality Form elements: text area, button, radio button, checkbox, dropdown list, etc. Date and time.
Pasewark & Pasewark 1 Word Lesson 3 Helpful Word Features Microsoft Office 2007: Introductory.
Cosc 4730 Android TabActivity and ListView. TabActivity A TabActivity allows for multiple “tabs”. – Each Tab is it’s own activity and the “root” activity.
Android Development (Basics)
Adding Controls to User Forms. Adding Controls A user form isn’t much use without some controls We’re going to add controls and write code for them Note.
Android Application Development Tutorial. Topics Lecture 5 Overview Overview of Networking Programming Tutorial 2: Downloading from the Internet.
Chapter 8: String Manipulation
Introduction to Android Programming Content Basic environmental structure Building a simple app Debugging.
PROG Mobile Java Application Development PROG Mobile Java Application Development Event Handling Creating Menus.
Android Hard & Soft Keyboards Notes are based on: The Busy Coder's Guide to Android Development by Mark L. Murphy Copyright © CommonsWare, LLC.
Mobile Computing Lecture#08 IntentFilters & BroadcastReceivers.
Mobile Programming Lecture 6
1 Announcements Homework #2 due Feb 7 at 1:30pm Submit the entire Eclipse project in Blackboard Please fill out the when2meets when your Project Manager.
@2011 Mihail L. Sichitiu1 Android Introduction GUI Menu Many thanks to Jun Bum Lim for his help with this tutorial.
Chapter 5 Menus, Common Dialog Boxes, and Methods Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.
Programming Mobile Applications with Android September, Albacete, Spain Jesus Martínez-Gómez.
Visual C# 2012 How to Program © by Pearson Education, Inc. All Rights Reserved.
Microsoft Office XP Illustrated Introductory, Enhanced Started with Windows 2000 Getting.
1 Introducing Activity and Intent. 2 Memory LinearLayout, weight=2 LinearLayout, weight=1 TextView ListView.
Visual C# 2012 How to Program © by Pearson Education, Inc. All Rights Reserved.
ANDROID – DRAWING IMAGES – SIMPLE EXAMPLE IN INTERFACE AND EVENT HANDLING L. Grewe.
Copyright© Jeffrey Jongko, Ateneo de Manila University Editing ListAdapter Data part 2.
Microsoft Office 2007: Introductory 1. Word – Lesson 3  Use automatic features including AutoCorrect, AutoFormat As You Type, Quick Parts, and AutoComplete.
Announcements Homework #2 will be posted after class due Thursday Feb 7, 1:30pm you may work with one other person No office hours tonight (sorry!) I will.
Copyright © Curt Hill More Components Varying the input of Dev-C++ Windows Programs.
GUI development with Matlab: GUI Front Panel Components GUI development with Matlab: Other GUI Components 1 Other GUI components In this section, we will.
Styles, Dialog Boxes, and Menus. Styles Allow creation of a common format – placed in res/values/styles.xml – file name is incidental Can be applied.
Menus Menus provide groups of related commands for windows applications Menu, menu items, submenus A menu item that contains a submenu is considered to.
Slide 1 Using Menu Bar & Common Dialog Boxes. Slide 2 Setting Up the Main Items v First open the form on which you want the menu located v Then start.
Mobile Programming Lecture 7 Dialogs, Menus, and SharedPreferences.
Copyright© Jeffrey Jongko, Ateneo de Manila University Deconstructing HelloWorld.
Android Application Lifecycle and Menus
CS378 - Mobile Computing More UI - Part 2. Special Menus Two special application menus – options menu – context menu Options menu replaced by action bar.
Customizing Menus and Toolbars CHAPTER 12 Customizing Menus and Toolbars.
Using Custom Submission Questions and Questionnaires in Editorial Manager™ Created by J. Strusz (9/21/2010)
Android 基本 I/O. 基本 I/O 介面元件 在此節中主要介紹常見的 I/O 使用者介 面元件 – Button, TextView, 以及 EditText , 學習者可以學會: – Android 的視窗表單設計 res/layout/main.xml – Android SDK –
School of Engineering and Information and Communication Technology KIT305/KIT607 Mobile Application Development Android OS –Permissions (cont.), Fragments,
Cosc 5/4730 Support design library. Support Design library Adds (API 9+) back support to a number of 5.0 lollipop widgets and material design pieces –
Lesson 7 Using Menus Victor Matos Cleveland State University Portions of this page are reproduced from work created and shared by Google and used according.
Fragments and Menus Chapter 4 1. Objectives Learn three different types of menus: options, context, and popup Learn to configure the ActionBar and Toolbar.
Lab7 – Appendix.
Android Programming - Features
Cleveland State University
Lecture 3 Zablon Ochomo Android Layouts Lecture 3 Zablon Ochomo
GUI Programming Fundamentals
Lesson 7 Using Menus Victor Matos Cleveland State University
Android – Event Handling
Android External Resources
Android Widgets 1 7 August 2018
ITEC535 – Mobile Programming
Politeknik Elektronika Negeri Surabaya
תכנות ב android אליהו חלסצ'י.
CIS 470 Mobile App Development
BMI Android Application will take weight and height from the users to calculate Body Mass Index (BMI) with the information, whether user is underweight,
CIS 470 Mobile App Development
Android Developer Fundamentals V2 Lesson 4
CIS 470 Mobile App Development
Android Notifications
Mobile Programmming Dr. Mohsin Ali Memon.
Android Sensor Programming
Android Sensor Programming
CIS 694/EEC 693 Android Sensor Programming
Presentation transcript:

Android Using Menus Notes are based on: The Busy Coder's Guide to Android Development by Mark L. Murphy Copyright © CommonsWare, LLC. ISBN: & Android Developers 8

2 8. Android – UI – Using Menus Using Menus 2 Menus usually increase the functionality of an app by providing additional operations on a small overlapping panel. Android provides two types of menu known as: options menu and context menu. 1.The options menu is triggered by pressing the hardware Menu button on the device, while 2.the context menu is raised by a tap-and-hold on the widget associated to the menu.

3 8. Android – UI – Using Menus Using Menus 3 Example: Using an option menu Press Menu button Options available in this context A max of six entries per menu. Excess will be displayed as part of the More option

4 8. Android – UI – Using Menus Using Menus 4 Example: Using an option menu Press Menu button Five available Options in this context

5 8. Android – UI – Using Menus Using Menus 5 Example : Dealing with SMS (text-messages) by using the built-in Messaging app’s context menu Available options Tap-&-Hold

6 8. Android – UI – Using Menus Using Menus 6 Observation: Option and Context Menus may include: 1.Text 2.Icons 3.Radio Buttons 4.Check Boxes 5.Sub-menus 6.Short-cut keys

7 8. Android – UI – Using Menus Using Menus 7 Example 1: Using an Option Menu Click on Menu button to see size options After choosing option: 50 points In this application the Option menu offers a way of changing the text size (on both EditText boxes)

8 8. Android – UI – Using Menus Using Menus 8 Example 1: Using an Option Menu Click the More button to see additional options Additional choices of the Option menu Up to six options will Be displayed on the Option menu. If there are more than six options pushing the More button will show the rest

9 8. Android – UI – Using Menus Using Menus 9 Example 1: Using a Context Menu Long-press a textbox to invoke its Context Menu Each view could have an associated Context Menu

10 8. Android – UI – Using Menus Using Menus 10 Example 1: Using Option and Context Menu The app shows two text boxes. Menus are used to change text’s size, color, and style. <LinearLayout xmlns:android=" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent“ > <EditText android:text="Hello world" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_margin = "5dp" /> <EditText android:text="Hola mundo" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_margin = "5dp" /> <TextView android:text="Press the MENU key, or \nLong-press text-boxes" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" />

11 8. Android – UI – Using Menus Using Menus 11 Example 1: Using Option and Context Menu The app shows two text boxes. Menus are used to change text’s size, color, and style. package cis493.matos.menu; // using Menus (Option & Context) import android.app.Activity; … public class Menu1Act1 extends Activity { EditText etMessage1; EditText etMessage2; Integer[] arrayPointSize = {10, 20, 30, 40, public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); etMessage1 = (EditText)findViewById(R.id.etMessage1); etMessage2 = (EditText)findViewById(R.id.etMessage2); // you may register an individual context menu for each view registerForContextMenu(etMessage1); registerForContextMenu(etMessage2); } //onCreate

12 8. Android – UI – Using Menus Using Menus 12 Example 1: Using Option and Context Menu The app shows two text boxes. Menus are used to change text’s size, color, and style. // set the option menu for the current public boolean onCreateOptionsMenu(Menu menu) { // only one Option menu per activity populateMyFirstMenu(menu); return super.onCreateOptionsMenu(menu); } // detect what view is calling and create its context public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { super.onCreateContextMenu(menu, v, menuInfo); // decide what context menu needs to be made if (v.getId() == etMessage1.getId()) // create a menu for etMessage1 box populateMyFirstMenu(menu); if (v.getId() == etMessage2.getId()){ // create a menu for etMessage2 box populateMySecondMenu(menu); } } //onCreateContextMenu

13 8. Android – UI – Using Menus Using Menus 13 Example 1: Using Option and Context Menu The app shows two text boxes. Menus are used to change text’s size, color, and style. private void populateMyFirstMenu(Menu menu){ int groupId = 0; int order= 0; //arguments: groupId, optionId, order, title menu.add(groupId, 1, 1, "10 points"); menu.add(groupId, 2, 2, "20 points"); menu.add(groupId, 3, 3, "30 points"); menu.add(groupId, 4, 4, "40 points"); menu.add(groupId, 5, 5, "50 points"); menu.add(groupId, 6, 8, "Red text"); menu.add(groupId, 7, 7, "Green Text"); menu.add(groupId, 8, 6, "Blue text"); } //populateMyMenu private void populateMySecondMenu(Menu menu){ int groupId = 0; int order= 0; //arguments: groupId, optionId, order, title menu.add(groupId, 9, 1, "Bold"); menu.add(groupId, 10, 2, "Italic"); menu.add(groupId, 11, 3, "Normal"); }//populateMySecondMenu

14 8. Android – UI – Using Menus Using Menus 14 Example 1: Using Option and Context Menu The app shows two text boxes. Menus are used to change text’s size, color, and style. // called whenever an item in your context menu is public boolean onContextItemSelected(MenuItem item) { return(applyMenuOption(item) || super.onContextItemSelected(item) ); } // called whenever an item in your options menu is public boolean onOptionsItemSelected(MenuItem item) { return(applyMenuOption(item) || super.onOptionsItemSelected(item) ); } Note: boolean event observers of the type onEvent(... ) by convention return true to indicate the triggered event has been consumed by the method, when false is returned the event is still alive and could be consumed by others.

15 8. Android – UI – Using Menus Using Menus 15 Example 1: Using Option and Context Menu // apply the action associated to selected item private boolean applyMenuOption(MenuItem item){ int menuItemId = item.getItemId(); // 1, 2, 3,...11 String strMsg2 = etMessage2.getText().toString(); if (menuItemId <= 5) { // first five option are for setting text size int newPointSize = arrayPointSize[menuItemId - 1]; etMessage1.setTextSize(newPointSize); etMessage2.setTextSize(newPointSize); } else { // either change color on box text1 or style on text2 if (menuItemId == 6) etMessage1.setTextColor(color.background_dark | Color.RED); etMessage1.setTextColor(0xffff0000); // red else if (menuItemId == 7) etMessage1.setTextColor(0xff00ff00); // green else if (menuItemId == 8) etMessage1.setTextColor(0xff0000ff); // blue else if (menuItemId == 9) etMessage2.setText(beautify(strMsg2, "BOLD")); //bold else if (menuItemId == 10) etMessage2.setText(beautify(strMsg2, "ITALIC")); //italic else if (menuItemId == 11) etMessage2.setText(beautify(strMsg2, "NORMAL")); //normal } return false; } //applyMenuOption

16 8. Android – UI – Using Menus Using Menus 16 Example 1: Using Option and Context Menu The app shows two text boxes. Menus are used to change text’s size, color, and style. // changing text style using HTML formatting // Spanned is text to which you could add formatting features private Spanned beautify (String originalText, String selectedStyle){ Spanned answer = null; if (selectedStyle.equals("BOLD")) answer = Html.fromHtml(" " + originalText +"</b"); else if (selectedStyle.equals("ITALIC")) answer = Html.fromHtml(" " + originalText +" "); else if (selectedStyle.equals("NORMAL")) answer = Html.fromHtml(" " + originalText +"</normal"); return answer; } //beautify } //Menu1Act1

17 8. Android – UI – Using Menus Using Menus 17 Comments on Creating an Option & Context Menu Step1. Indicate which widget(s) on your activity have context menus. To do this, call registerForContextMenu(theWidget) supplying the View that is the widget needing a context menu. Step2. Implement onCreateContextMenu(…), populate your menu adding text, icons, etc. to the different options. Use input menu parameter to determine which menu to build (assuming your activity has more than one). The onCreateContextMenu() method gets the ContextMenu itself, the View the context menu is associated with, and a ContextMenu.ContextMenuInfo, which tells you which item in the list the user did the tap-and-hold over, in case you want to customize the context menu based on that information

18 8. Android – UI – Using Menus Using Menus 18 Comments on Creating an Option & Context Menu onCreateContextMenu() is called each time the context menu is requested. Unlike the options menu (which is only built once per activity), context menus are discarded once they are used or dismissed. To find out when a context menu choice was chosen, implement onContextItemSelected() on the activity.

19 8. Android – UI – Using Menus Using Menus 19 Comments on Creating an Option & Context Menu In Example1 the event observers: onOptionsItemSelected() (for options menu selections) and onContextItemSelected() (for context menu selections) delegate on the applyMenuChoice(…) method the performing of the actions associated to the choices made by the public boolean onOptionsItemSelected(MenuItem item) { return (applyMenuChoice(item) ); public boolean onContextItemSelected(MenuItem item) { return (applyMenuChoice(item)); }

20 8. Android – UI – Using Menus Using Menus 20 Example 2: Enhancing Option/Context Menu Click on SubMenu to see additional options This extension of the previous example adds icons and links to the Option Menu entries, as well as a sub-menu list tied to one the option menu buttons. Extending Example1. Keyboard shortcuts / links

21 8. Android – UI – Using Menus Using Menus 21 Example 2: Enhancing Option/Context Menu This extension of the previous example adds icons and links to the Option Menu entries, as well as a sub-menu list tied to one the option menu buttons. Extending Example1.

22 8. Android – UI – Using Menus Using Menus 22 Example 2: Enhancing Option/Context Menu private void populateMyFirstMenu(Menu menu){ int groupId = 0; //arguments: groupId, optionId, order, title MenuItem item1 = menu.add(groupId, 1, 1, "10 points"); MenuItem item2 = menu.add(groupId, 2, 2, "20 points"); MenuItem item3 = menu.add(groupId, 3, 3, "30 points"); MenuItem item4 = menu.add(groupId, 4, 4, "40 points"); //MenuItem item5 = menu.add(groupId, 5, 5, "50 points"); MenuItem item6 = menu.add(groupId, 6, 8, "Red text"); MenuItem item7 = menu.add(groupId, 7, 7, "Green Text"); MenuItem item8 = menu.add(groupId, 8, 6, "Blue text"); //set icons item1.setIcon(R.drawable.uno); item2.setIcon(R.drawable.dos); item3.setIcon(R.drawable.tres); item4.setIcon(R.drawable.cuatro); // shortcuts using device’s keyboard-keypad // on a G1 open slide open the keyboard and // type letter u (same as pressing menu UNO) item1.setShortcut('1', '1'); item2.setShortcut('2', '2'); item3.setShortcut('3', '3'); item4.setShortcut('4', '4'); Extending Example1. Figures used in this example were taken from: C:\android-sdk-windows\platforms\android-4\data\res\drawable Replace the method populateMyFirstMenu with the following code Remove this line from previous version

23 8. Android – UI – Using Menus Using Menus 23 Example 2: Enhancing Option/Context Menu // adding a sub-menu as fifth entry of this menu //.addSubMenu(int groupId, int itemId, int order, CharSequence title) int smGroupId = 0; // don't care, same as Menu.NONE int smItemId = 5; // fifth element int smOrder = 5; // don't care, same as Menu.NONE SubMenu mySubMenu5 = menu.addSubMenu(smGroupId, smItemId, smOrder, "Sub-Menu-CINCO"); mySubMenu5.setHeaderIcon(R.drawable.btn_rating_star_on_pressed); mySubMenu5.setIcon(R.drawable.cinco); //.add(int groupId, int itemId, int order, CharSequence title) MenuItem sub51 = mySubMenu5.add(smGroupId,5,1,"Sub Menu 5-1"); MenuItem sub52 = mySubMenu5.add(smGroupId,5,2,"Sub Menu 5-2"); MenuItem sub53 = mySubMenu5.add(smGroupId,5,3,"Sub Menu 5-3"); } //populateMyFirstMenu Extending Example1. Continuation… Replace the method populateMyFirstMenu with the following code

24 8. Android – UI – Using Menus Using Menus 24 Example 2: Enhancing Option/Context Menu private boolean applyMenuOption(MenuItem item){ int menuItemId = item.getItemId(); //1, 2, 3,...11 String strMsg2 = etMessage2.getText().toString(); if (menuItemId < 5) { // first four options are for setting text size int newPointSize = arrayPointSize[menuItemId - 1]; etMessage1.setTextSize(newPointSize); etMessage2.setTextSize(newPointSize); } else if (menuItemId == 5) { // the sub-menu (attached to 5th item) is processed here etMessage1.setText ( "You have selected: \n" +item.getTitle() + "\nId: " + menuItemId + " order: " + item.getOrder() ); } // either change color on text1 or style on text2 else if (menuItemId == 6) etMessage1.setTextColor(0xffff0000); // red Extending Example1. Continuation… Replace the method applyMenuOption with the following code Same as before Take care of sub- menu here

25 8. Android – UI – Using Menus Using Menus 25 Example 2: Enhancing Option/Context Menu else if (menuItemId == 7) etMessage1.setTextColor(0xff00ff00); // green else if (menuItemId == 8) etMessage1.setTextColor(0xff0000ff); // blue else if (menuItemId == 9) etMessage2.setText(beautify(strMsg2, "BOLD")); //bold else if (menuItemId == 10) etMessage2.setText(beautify(strMsg2, "ITALIC")); //italic else if (menuItemId == 11) etMessage2.setText(beautify(strMsg2, "NORMAL")); //normal return false; } //applyMenuOption Extending Example1. Continuation… Replace the method applyMenuOption with the following code Same as before

26 8. Android – UI – Using Menus Using Menus 26 Questions ?