Mobile Application Development CHAPTER 2.pptx

kahsay10 1 views 53 slides Oct 16, 2025
Slide 1
Slide 1 of 53
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35
Slide 36
36
Slide 37
37
Slide 38
38
Slide 39
39
Slide 40
40
Slide 41
41
Slide 42
42
Slide 43
43
Slide 44
44
Slide 45
45
Slide 46
46
Slide 47
47
Slide 48
48
Slide 49
49
Slide 50
50
Slide 51
51
Slide 52
52
Slide 53
53

About This Presentation

Mobile application development short notes


Slide Content

Chapter 2 1

Menus, Dialogues, Lists S L I D E B A Z A A R | 2

S L I D E B A Z A A R | Appear whenever the user presses the menu button. Useful for giving different options without leaving the current Activity Don’t make too big menus, or they’ll cover entirely the Activity There are 3 types : Options menu → Is the primary collection of menu items for an activity. → It's where you should place actions that have a global impact on the app, such as "Search," "Compose email," and "Settings." Popup menu → Displays a list of items in a vertical list that's anchored to the view that invoked the menu. Context menu → Is a floating menu that appears when the user performs a long- click on an element. → It provides actions that affect the selected content or context frame . 3

S L I D E B A Z A A R | /ML Place a file inside res/menu/ Inflate the menu inside the Activity Useful if you want to create the same menu inside different activities Java : Create the menu directly inside the activity o For all menu types, Android provides a standard /ML format to define menu items. Define a menu and all its items in an /ML menu resource then inflate the menu resource (load it as a Menu object) in activity. Using a menu resource is a good practice for a few reasons: → It's easier to visualize the menu structure in /ML. → It separates the content for the menu from application's behavioral code. → It allows to create alternative menu configurations for different platform versions, screen sizes, and other configurations by leveraging the app resources framework. Two methods (again) Defining a Menu in /ML 4

S L I D E B A Z A A R | → To define the menu, create an /ML file inside project's res/menu/ directory and build the menu with the following elements: <menu> Defines a Menu, which is a container for menu items. A <menu> element must be the root node for the file and can hold one or more <item> and <group> elements. <item> Creates a MenuItem, which represents a single item in a menu. It may contain a nested <menu>element in order to create a submenu. <group> An optional, invisible container for <item> elements. It allows you to categorize menu items so they share properties such as active state and visibility. menu.xml 5

S L I D E B A Z A A R | The <item> element supports several attributes you can use to define an item's appearance and behavior. The items element has the following important attributes: ⌐ android:id :[- A resource ID that's unique to the item, which allows the application can recognize the item when the user selects it. ⌐ android:icon :[- reference to a drawable to use as the item's icon. ⌐ android:title :[- A reference to a string to use as the item's title. ⌐ android:showAsAction : [- Specifies when and how this item should appear as an action item in the action bar. ⌐ You can add a submenu to an item in any menu (except a submenu) by adding a <menu> element as the child of an <item>. ⌐ Submenus are useful when your application has a lot of functions that can be organized into topics, like items in a PC application's menu bar (File, Edit, View, etc.). 6

S L I D E B A Z A A R | 7

S L I D E B A Z A A R | The options menu is where you should include actions and other options that are relevant to the current activity context, such as "Search," "Compose email," and "Settings.“ To specify the options menu for an activity, override onCreateOptionsMenu(). In this method, you can inflate your menu resource (defined in /ML) into the Menu provided in the callback. √ You can also add menu items using add() and retrieve items with findItem() to revise their properties with MenuItem APIs. 8

S L I D E B A Z A A R | When the user selects an item from the options menu (including action items in the action bar), the system calls your activity's onOptionsItemSelected() method, which passes the MenuItem selected. You can identify the item by calling getItemId(), which returns the unique ID for the menu item (defined by the android:id attribute in the menu resource or with an integer given to the add() method). You can match this ID against known menu items to perform the appropriate action. When you successfully handle a menu item, return true. If you don't handle the menu item, you should call the superclass implementation of onOptionsItemSelected() (the default implementation returns false). 9

S L I D E B A Z A A R | 10

S L I D E B A Z A A R | A contextual menu offers actions that affect a specific item or context frame in the UI. You can provide a context menu for any view, but they are most often used for items in a ListView, GridView, or other view collections in which the user can perform direct actions on each item. Screenshots of a floating context menu (left) and the contextual action bar (right). 11

S L I D E B A Z A A R | There are two ways to provide contextual actions: In a floating context menu. A menu appears as a floating list of menu items (similar to a dialog) when the user performs a long- click (press and hold) on a view that declares support for a context menu. Users can perform a contextual action on one item at a time. In the contextual action mode. This mode is a system implementation of ActionMode that displays a contextual action bar at the top of the screen with action items that affect the selected item(s). When this mode is active, users can perform an action on multiple items at once (if your app allows it). To provide a floating context menu: Register the View to which the context menu should be associated by calling registerForContextMenu()and pass it the View. If your activity uses a ListView or GridView and you want each item to provide the same context menu, register all items for a context menu by passing the ListView or GridView to registerForContextMenu(). Implement the onCreateContextMenu() method in your Activity. When the registered view receives a long- click event, the system calls your onCreateContextMenu()method. This is where you define the menu items, usually by inflating a menu resource. 12

S L I D E B A Z A A R | MenuInflater allows you to inflate the context menu from a menu resource. The callback method parameters include the View that the user selected and a ContextMenu. ContextMenuInfo object that provides additional information about the item selected. If your activity has several views that each provide a different context menu, you might use these parameters to determine which context menu to inflate. 13

S L I D E B A Z A A R | Implement onContextItemSelected( ): When the user selects a menu item, the system calls this method so you can perform the appropriate action. 14

S L I D E B A Z A A R | 15

S L I D E B A Z A A R | A PopupMenu is a modal menu anchored to a View. It appears below the anchor view if there is room, or above the view otherwise. It's useful for: Providing an overflow- style menu for actions that relate to specific content (such as Gmail's email headers). Providing a second part of a command sentence (such as a button marked "Add" that produces a popup menu with different "Add" options). Providing a drop- down similar to Spinner that does not retain a persistent selection. If you define your menu in /ML, here's how you can show the popup menu: Instantiate a PopupMenu with its constructor, which takes the current application Context and the View to which the menu should be anchored. Use MenuInflater to inflate your menu resource into the Menu object returned by using PopupMenu.inflate(). Call PopupMenu.show(). 16

S L I D E B A Z A A R | 17

S L I D E B A Z A A R | To perform an action when the user selects a menu item, you must implement the PopupMenu.OnMenuItemClickListener interface and register it with your PopupMenu by calling setOnMenuItemclickListener(). When the user selects an item, the system calls the onMenuItemClick() callback in your interface. 18

S L I D E B A Z A A R | 19

S L I D E B A Z A A R | 20

S L I D E B A Z A A R | A menu group is a collection of menu items that share certain traits. With a group, you can: Show or hide all items with setGroupVisible() Enable or disable all items with setGroupEnabled() Specify whether all items are checkable with setGroupCheckable() You can create a group by nesting <item> elements inside a <group> element in your menu resource or by specifying a group ID with the add() method. 21

S L I D E B A Z A A R | A menu can be useful as an interface for turning options on and off, using a checkbox for stand- alone options, or radio buttons for groups of mutually exclusive options. You can define the checkable behavior for individual menu items using the android:checkable attribute in the <item> element, or for an entire group with the android:checkableBehavior attribute in the <group> element. 22

S L I D E B A Z A A R | The android:checkableBehavior attribute accepts either: Single : Only one item from the group can be checked (radio buttons) all : All items can be checked (checkboxes) none : No items are checkable You can apply a default checked state to an item using the android:checked attribute in the <item> element and change it in code with the setChecked() method. When a checkable item is selected, the system calls your respective item- selected callback method (such as onOptionsItemSelected()). You can query the current state of the item (as it was before the user selected it) with isChecked() and then set the checked state with setChecked(). 23

S L I D E B A Z A A R | 24

S L I D E B A Z A A R | A dialog is a small window that prompts the user to make a decision or enter additional information. A dialog does not fill the screen and is normally used for modal events that require users to take an action before they can proceed. 25

S L I D E B A Z A A R | The Dialog class is the base class for dialogs, but you have to use one of the following subclasses: AlertDialog : A dialog that can show a title, up to three buttons, a list of selectable items, or a custom layout. DatePickerDialog or TimePickerDialog: A dialog with a pre- defined UI that allows the user to select a date or time. These classes define the style and structure for your dialog, but you should use a DialogFragment as a container for your dialog. The DialogFragment class provides all the controls you need to create your dialog and manage its appearance, instead of calling methods on the Dialog object. Using DialogFragment to manage the dialog ensures that it correctly handles lifecycle events such as when the user presses the Back button or rotates the screen. The DialogFragment class also allows you to reuse the dialog's UI as an embeddable component in a larger UI. 26

S L I D E B A Z A A R | You can accomplish a wide variety of dialog designs by extending DialogFragment and creating a AlertDialog in the onCreateDialog() callback method. 27

S L I D E B A Z A A R | The AlertDialog class allows you to build a variety of dialog designs and is often the only dialog class you'll need. As shown in figure, there are three regions of an alert dialog: Title : This is optional and should be used only when the content area is occupied by a detailed message, a list, or custom layout. If you need to state a simple message or question, you don't need a title. Content area : This can display a message, a list, or other custom layout. Action buttons : There should be no more than three action buttons in a dialog. 28

S L I D E B A Z A A R | To add action buttons, call the setPositiveButton() and setNegativeButton() methods: 29

S L I D E B A Z A A R | The set...Button() methods require a title for the button (supplied by a string resource) and a DialogInterface.OnClickListener that defines the action to take when the user presses the button. There are three different action buttons you can add: Positive : You should use this to accept and continue with the action (the "OK" action). Negative : You should use this to cancel the action. Neutral : You should use this when the user may not want to proceed with the action, but doesn't necessarily want to cancel. It appears between the positive and negative buttons. For example, the action might be "Remind me later.“ You can add only one of each button type to an AlertDialog. That is, you cannot have more than one "positive" button. 30

S L I D E B A Z A A R | There are three kinds of lists available with the AlertDialog APIs: A traditional single- choice list A persistent single- choice list (radio buttons) A persistent multiple- choice list (checkboxes) To create a single- choice list like the one in figure, use the setItems() method: 31

S L I D E B A Z A A R | To add a list of multiple- choice items (checkboxes) or single- choice items (radio buttons), use the setMultiChoiceItems() or setSingleChoiceItems()methods, respectively. For example, here's how you can create a multiple- choice list like the one shown in figure that saves the selected items in an ArrayList: 32

S L I D E B A Z A A R | 33

Notifications → Display Elements → Creating Notifications → Notification Actions → Managing Notifications → Removing Notifications → Displaying Progress Toasts → The Basics → Positioning a Toast S L I D E B A Z A A R | 34

Notifications A notification is a message you can display to the user outside of your application's normal UI. When you tell the system to issue a notification, it first appears as an icon in the notification area . To see the details of the notification, the user opens the notification drawer . Both the notification area and the notification drawer are system- controlled areas that the user can view at any time. S L I D E B A Z A A R | 35

….cont Figure 1. Notifications in the notification area. Figure 2. Notifications in the notification drawer. S L I D E B A Z A A R | 36

Notification Display Elements Notifications in the notification drawer can appear in one of two visual styles, depending on the version and the state of the drawer: Normal view: The standard view of the notifications in the notification drawer. Big view: A large view that's visible when the notification is expanded. Big view is part of the expanded notification feature available as of Android 4.1. S L I D E B A Z A A R | 37

Normal view A notification in normal view appears in an area that's up to 64 dp tall. Even if you create a notification with a big view style, it will appear in normal view until it's expanded. An example of a normal view: Figure 3. Notification in normal view. The callouts in the illustration refer to the following: Content title Large icon Content text Content info Small icon Time that the notification was issued S L I D E B A Z A A R | 38

Big view A notification's big view appears only when the notification is expanded, which happens when the notification is at the top of the notification drawer, or when the user expands the notification with a gesture. Expanded notifications are available starting with Android 4.1. The following screenshot shows an inbox- style notification: Figure 4. Big view notification. S L I D E B A Z A A R | 39

…cont Notice that the big view shares most of its visual elements with the normal view. The only difference is callout number 7, the details area. Each big view style sets this area in a different way. The available styles are: Big picture style: The details area contains a bitmap up to 256 dp tall in its detail section. Big text style: Displays a large text block in the details section. Inbox style: Displays lines of text in the details section. All of the big view styles also have the following content options that aren't available in normal view: Big content title: Allows you to override the normal view's content title with a title that appears only in the expanded view. Summary text: Allows you to add a line of text below the details area. S L I D E B A Z A A R | 40

Creating a Notification Notification in android are represented by the Notification class. To create notification use NotificationManager class which can be received from the Context, eg.an activity or a service ,via GetSystemService() method. The Notification.Builder provides an builder interface to create an Notification object PendingIntent to specify the action which should be performed once the user select the notification. Required notification contents A Notification object must contain the following: A small icon, set by setSmallIcon() A title, set by setContentTitle() Detail text, set by setContentText() The Notification.Builder provides an builder interface to create an Notification object S L I D E B A Z A A R | 41

....cont S L I D E B A Z A A R | 42

Managing Notifications When you need to issue a notification multiple times for the same type of event, you should avoid making a completely new notification. Instead, you should consider updating a previous notification, either by changing some of its values or by adding to it, or both. For example, Gmail notifies the user that new emails have arrived by increasing its count of unread messages and by adding a summary of each email to the notification. S L I D E B A Z A A R | 43

Removing notifications Notifications remain visible until one of the following happens: The user dismisses the notification either individually or by using "Clear All" (if the notification can be cleared). The user clicks the notification, and you called setAutoCancel() when you created the notification. You call cancel() for a specific notification ID. This method also deletes ongoing notifications. You call cancelAll(), which removes all of the notifications you previously issued. S L I D E B A Z A A R | 44

Toasts A toast provides simple feedback about an operation in a small popup. It only fills the amount of space required for the message and the current activity remains visible and interactive. Toasts automatically disappear after a timeout. For example, navigating away from an email before you send it triggers a "Draft saved" toast to let you know that you can continue editing later. S L I D E B A Z A A R | 45

The Basics First, instantiate a Toast object with one of the makeText() methods. This method takes three parameters: the application Context, the text message, and the duration for the toast. It returns a properly initialized Toast object. You can display the toast notification with show(), as shown in the following example: S L I D E B A Z A A R | 46

Positioning your Toast A standard toast notification appears near the bottom of the screen, centered horizontally. You can change this position with the setGravity(int, int, int) method. This accepts three parameters: a Gravity constant, an x- position offset, and a y- position offset. For example, if you decide that the toast should appear in the top- left corner, you can set the gravity like this: If you want to nudge the position to the right, increase the value of the second parameter. To nudge it down, increase the value of the last parameter. S L I D E B A Z A A R | 47

S L I D E B A Z A A R | 48

SMS messaging is one of the main killer applications on a mobile phone today — for some users as necessary as the phone itself. Any mobile phone you buy today should have at least SMS messaging capabilities, and nearly all users of any age know how to send and receive messages. Android comes with a built- in SMS application that enables you to send and receive SMS messages. However, in some cases you might want to integrate SMS capabilities into your own Android application. For example, you might want to write an application that automatically sends a SMS message at regular time intervals. For example, this would be useful if you wanted to track the location of your kids — simply give them an Android device that sends out an SMS message containing its geographical location every 30 minutes. Now you know if they really went to the library after school! (Of course, that would also mean you would have to pay the fees incurred in sending all those SMS messages…) In AndroidManifest./ML code add: <uses- permission android:name = ”android.permission.SEND_SMS”></uses- permission> Sending SMS Message S L I D E B A Z A A R | 49

Sending SMS Message Programmatically public class MainActivity extends Activity{ Button btnSendSMS; @Override Public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.main); btnSendSMS= (Button) findViewById(R.id.btnSendSMS); btnSendSMS.setOnClickListener(new View.OnClickListener(){ public void onClick(View v){ sendSMS(“5556”, “Hello my friends!”); } }); } //- -- sends an SMS message to another device- -- private public void sendSMS(String phoneNumber, String message) { SmsManager sms = SmsManager.getDefault(); sms.sendTextMessage(phoneNumber, null, message, null, null); } S L I D E B A Z A A R | 50

How it works? Android uses a permissions- based policy whereby all the permissions needed by an application must be specified in the AndroidManifest.xml file. This ensures that when the application is installed, the user knows exactly which access permissions it requires. Because sending SMS messages incurs additional costs on the user’s end, indicating the SMS permissions in the AndroidManifest.xml file enables users to decide whether to allow the application to install or not. To send an SMS message programmatically, you use the SmsManager class. Unlike other classes, you do not directly instantiate this class; instead, you call the getDefault() static method to obtain a SmsManager object. You then send the SMS message using the sendTextMessage() method: Following are the five arguments to the sendTextMessage()method: destinationAddress Phone number of the recipient scAddress — Service center address; use null for default SMSC text — Content of the SMS message sentIntent — Pending intent to invoke when the message is sent deliveryIntent — Pending intent to invoke when the message has been delivered. SmsManagerObject. sendTextMessage( destinationAddress scAddress , text , sentIntent , deliveryIntent ); String SENT = “SMS_SENT”; String DELIVERED = “SMS_DELIVERED”; PendingIntent sentPI = PendingIntent.getBroadcast(this, 0,new Intent(SENT),0); PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0, newIntent(DELIVERED), 0); S L I D E B A Z A A R | 51

S L I D E B A Z A A R | 52

THANKYOU 53
Tags