Mobile Computing and Wireless Communication 2170710 Unit 6

Mobile Computing and Wireless Communication (2170710) Unit – 6: Android Programming Prof. Maulik Trivedi � 9998265805 � maulik. trivedi@darshan. ac. in Computer Engineering Darshan Institute of Engineering & Technology

Topics to be covered § Facts § Android App § Android Platform § Android Application Framework § Android SDK and Studio § Android App Components § Create App § Android Version History § Activity Life Cycle § Activity, Layout and Controls Unit: 6 – Android Programming 2 Darshan Institute of Engineering & Technology

Facts Unit: 6 – Android Programming 3 Darshan Institute of Engineering & Technology

Android APP Intent Unit: 6 – Android Programming 4 Darshan Institute of Engineering & Technology

Android Platform § Comprise of Core Components and § Also, ✔ APIs ✔ Supporting files and libraries, … Unit: 6 – Android Programming 5 Darshan Institute of Engineering & Technology

System Apps Android Application Framework Dialer Email Calendar Camera . . . Java API Framework Managers Content Providers Activity Resource View Systems Package Location Telephony Libc Open. MAX AL Media Framework Open. GL ES Window Android Runtime Core Libraries Android Runtime (ART) NATIVE C/C++ Libraries Webkit Notification . . . Hardware Abstraction Layer (HAL) Audio Bluetooth Camera Sensors . . . Linux Kernel Drivers Audio Binder (IPC) Display Keypad Bluetooth Camera Shared Memory USB WIFI Power Management

Application Framework View System Use it to build an app’s UI, including lists, grids, text boxes, buttons, and even an embeddable web browser. Resource Manager It gives access to non-code resources such as localized strings, graphics, and layout files. Notification Manager It enables all apps to display custom alerts in the status bar. Activity Manager It manages the lifecycle of apps and provides a common navigation back stack Content Provider It enable apps to access data from other apps, such as the Contacts app, or to share their own data. Unit: 6 – Android Programming 7 Darshan Institute of Engineering & Technology

Android SDKs SDK Platform One for each version of Android SDK Tools for debugging and testing, and other useful utilities. Sample Apps Code examples to help you understand how to use some of the APIs, etc. , Documentation API Documentation Android Support Extra APIs that aren’t available in the standard platform Google Play Billing Allows you to integrate billing service in you app. Unit: 6 – Android Programming 8 Darshan Institute of Engineering & Technology

Download Android Studio § https: //developer. android. com/studio/install? pkg=studio Unit: 6 – Android Programming 9 Darshan Institute of Engineering & Technology

Android App Components Services Activities Broadcast Receivers Unit: 6 – Android Programming Content Providers 10 Darshan Institute of Engineering & Technology

Create a Android Project § Start and configure the android project Unit: 6 – Android Programming 11 Darshan Institute of Engineering & Technology

Create a Android Project - Cont… § Select the form factor and API Level Unit: 6 – Android Programming 12 Darshan Institute of Engineering & Technology

Add an Activity Unit: 6 – Android Programming 13 Darshan Institute of Engineering & Technology

Configure an Activity Unit: 6 – Android Programming 14 Darshan Institute of Engineering & Technology

Android Version History Code Name Version Number Linux Kernel Version API Level Ø No Codename 1. 0 ? 1 Ø Petit Four 1. 1 2. 6. X 2 Ø Cupcake 1. 5 2. 6. 27 3 Ø Donut 1. 6 2. 6. 29 4 Ø Eclair 2. 0 -2. 1 2. 6. 29 5 -7 Ø Froyo 2. 2 -2. 2. 3 2. 6. 32 8 Ø Gingerbread 2. 3 -2. 3. 7 2. 6. 35 9 -10 Ø Honeycomb 3. 0 -3. 2. 6. 36 11 -13 Ø Ice Cream Sandwich 4. 0 -4. 0. 4 3. 0. 1 14 -15 Ø Jelly Bean 4. 1 -4. 3. 1 3. 0. 31 - 3. 4. 39 16 -18 Ø Kit. Kat 4. 4 -4. 4. 4 3. 10 19 -20 Ø Lollipop 5. 0 -5. 1. 1 3. 16. 1 21 -22 Ø Marshmallow 6. 0 -6. 0. 1 3. 18. 10 23 Ø Nougat 7. 0 -7. 1. 2 4. 4. 1 24 -25 Ø Oreo 8. 0 -8. 1 4. 10 26 -27 Ø Pie 9. 0 4. 4. 107, 4. 9. 84 & 4. 14. 42 28 https: //en. wikipedia. org/wiki/Android_version_history Unit: 6 – Android Programming 15 Darshan Institute of Engineering & Technology

Activities & Layouts The device launches your app and creates an activity object. Device 1 The activity object specifies a layout. 2 Layouts Activity Layouts The activity tells Android to display the layout on screen. 3 The activity responds to these interactions by running application code. The user interacts with the layout that is displayed on the device. 5 4 Activity User 7 which the user sees on the device. Unit: 6 – Android Programming Device 16 6 The activity updates the display Darshan Institute of Engineering & Technology

Android Studio Project Structure Files What it is for? Java and XML source files Activity and layout files Android-generated Java Automatically generated files Unit: 6 – Android Programming Resource files Images, icons, style, string Configuration files What actually in the app and how it should run 17 Darshan Institute of Engineering & Technology

Android. Manifest. xml § App Package Name § App Components ✔ Activities ✔ Services ✔ Broadcast receivers ✔ Content providers § Permissions § Hardware and Software features Unit: 6 – Android Programming 18 Darshan Institute of Engineering & Technology

Activity launched Activity Lifecycle on. Create() on. Start() User navigates to the activity on. Restart() on. Resume() App process killed Activity running Another activity comes into the foreground Apps with higher priority need memory User returns to the activity on. Pause() The activity is no longer visible on. Stop() The activity is finishing or being destroyed by the system on. Destroy() Activity shut down User navigates to the activity

Layouts § A layout defines the structure for a user interface in your app, such in an activity. View. Group Unit: 6 – Android Programming View. Group View 20 View Darshan Institute of Engineering & Technology

Common Layouts § Each subclass of the View. Group class provides a unique way to display the views you nest within it. § Common layout types are: ✔ Linear Layout ✔ Relative Layout ✔ Web View ✔ List View ✔ Grid View Unit: 6 – Android Programming 21 Darshan Institute of Engineering & Technology

Linear Layout § Linear. Layout is a view group that aligns all children in a single direction, vertically, or horizontally § You can specify the layout direction with the android: orientation attribute Unit: 6 – Android Programming 22 Darshan Institute of Engineering & Technology

Linear Layout <? xml version="1. 0" encoding="utf-8"? > <Linear. Layout xmlns: android=“http: //schemas. android. com/apk/res/android" android: layout_width="match_parent" android: layout_height="match_parent" android: padding. Left="16 dp" android: padding. Right="16 dp" android: orientation="vertical" > <Edit. Text android: layout_width="match_parent" android: layout_height="wrap_content" android: hint="@string/to" /> <Edit. Text android: layout_width="match_parent" android: layout_height="wrap_content" android: hint="@string/subject" /> <Edit. Text android: layout_width="match_parent" android: layout_height="0 dp" android: layout_weight="1" android: gravity="top" android: hint="@string/message" /> <Button android: layout_width="100 dp" android: layout_height="wrap_content" android: layout_gravity="right" android: text="@string/send" /> </Linear. Layout> Unit: 6 – Android Programming 23 Darshan Institute of Engineering & Technology

Relative Layout § Relative. Layout is a view group that displays child views in relative positions. § The position of each view can be specified as relative to sibling elements or in positions relative to the parent Relative. Layout area. Unit: 6 – Android Programming 24 Darshan Institute of Engineering & Technology

<? xml version="1. 0" encoding="utf-8"? > <Relative. Layout xmlns: android="http: //schemas. android. com/apk/res/android" android: layout_width="match_parent" android: layout_height="match_parent" android: padding. Left="16 dp" android: padding. Right="16 dp" > <Edit. Text android: id="@+id/name" android: layout_width="match_parent" android: layout_height="wrap_content" android: hint="@string/reminder" /> <Spinner android: id="@+id/dates" android: layout_width="0 dp" android: layout_height="wrap_content" android: layout_below="@id/name" android: layout_align. Parent. Left="true" android: layout_to. Left. Of="@+id/times" /> <Spinner android: id="@id/times" android: layout_width="96 dp" android: layout_height="wrap_content" android: layout_below="@id/name" android: layout_align. Parent. Right="true" /> <Button android: layout_width="96 dp" android: layout_height="wrap_content" android: layout_below="@id/times" android: layout_align. Parent. Right="true" android: text="@string/done" /> </Relative. Layout> Linear Layout

Scroll. View Layout § A view group that allows the view hierarchy placed within it to be scrolled is Scroll. View. § Scroll view supports vertical scrolling only. For horizontal scrolling, use Horizontal. Scroll. View. Unit: 6 – Android Programming 26 Layout 1 Scroll. View § To add multiple views within the scroll view, make the direct child you add a view group, e. g. , Linear. Layout, & place additional views within that Linear. Layout. Screen Size § Scroll view may have only one direct child placed within it. Layout 2 Darshan Institute of Engineering & Technology

Scroll. View Layout <Scroll. View xmlns: android="http: //schemas. android. com/apk/res/android" android: layout_width="match_parent" android: layout_height="match_parent" android: fill. Viewport="true"> <Table. Layout android: layout_width="match_parent" android: layout_height="match_parent" android: stretch. Columns="1"> <!-- everything you already have --> </Table. Layout> </Scroll. View> Unit: 6 – Android Programming 27 Darshan Institute of Engineering & Technology

Table Layout § A layout that arranges its children into rows and columns. § A Table. Layout consists of a Table. Row objects, each defining a row. § Table. Layout containers do not display border lines of their rows, columns or cells. § Each row has zero or more cells; each cell can hold one View object. § The table has as many columns as the row with the most cells. A table can leave cells empty. Cells can span columns, as they can be in HTML. Unit: 6 – Android Programming 28 Row 1 Row 2 Column 2 Row 3 Column 1 Row 2 Column 3 Row 3 Column 2 Darshan Institute of Engineering & Technology

Table Layout <? xml version="1. 0" encoding="utf-8"? > <Table. Layout xmlns: android="http: //schemas. android. com/apk/res/android" android: layout_width="match_parent" android: layout_height="match_parent" android: stretch. Columns="1"> <Table. Row> <Text. View android: text="@string/table_layout_4_open" android: padding="3 dip" /> <Text. View android: text="@string/table_layout_4_open_shortcut" android: gravity="right" android: padding="3 dip" /> </Table. Row> <Table. Row> <Text. View android: text="@string/table_layout_4_save" android: padding="3 dip" /> <Text. View android: text="@string/table_layout_4_save_shortcut" android: gravity="right" android: padding="3 dip" /> </Table. Row> </Table. Layout> Unit: 6 – Android Programming 29 Darshan Institute of Engineering & Technology

Frame Layout § It is designed to block out an area on the screen to display a single item. § It should be used to hold a single child view, because it can be difficult to organise child views in a way that’s scalable to different screen sizes without the children overlapping each other. § You can, however, add multiple children to a Frame. Layout and control their position within the Frame. Layout by assigning gravity to each child, using the android: layout_gravity attribute. Frame Layout Linear Layout Edit. Text Image. View Button Unit: 6 – Android Programming 30 Darshan Institute of Engineering & Technology

Frame Layout <Frame. Layout xmlns: android="http: //schemas. android. com/apk/res/android" android: layout_width="fill_parent" android: layout_height="fill_parent"> <Image. View android: src="@drawable/ic_launcher" android: scale. Type="fit. Center" android: layout_height="250 px" android: layout_width="250 px"/> <Text. View android: text="Frame Demo" android: text. Size="30 px" android: text. Style="bold" android: layout_height="fill_parent" android: layout_width="fill_parent" android: gravity="center"/> </Frame. Layout> Unit: 6 – Android Programming 31 Darshan Institute of Engineering & Technology

Text. View § A user interface element that displays text to the user, is Text. View This is a Text. View <Linear. Layout xmlns: android="http: //schemas. android. com/apk/res/android" android: layout_width="match_parent" android: layout_height="match_parent"> <Text. View android: id="@+id/text_view_id" android: layout_height="wrap_content" android: layout_width="wrap_content" android: text="@string/hello" /> </Linear. Layout> Unit: 6 – Android Programming 32 Darshan Institute of Engineering & Technology

Edit. Text § A user interface element for entering and modifying text. § When you define an edit text widget, you must specify the R. styleable. Text. View_input. Type attribute. § Choosing the input type configures the keyboard type that is shown, acceptable characters, and appearance of the edit text. § E. g. input. Type to “phone” <Edit. Text android: id="@+id/plain_text_input" android: layout_height="wrap_content" android: layout_width="match_parent" android: input. Type="text"/> Unit: 6 – Android Programming This is a Text. View 33 Darshan Institute of Engineering & Technology

Button § A button consists of text or an icon that communicates what action occurs when the user touches it. § Depending on whether you want a button with text, an icon, or both, you can create the button in your layout in three ways: Button Unit: 6 – Android Programming Button 34 Darshan Institute of Engineering & Technology

Button – Cont… With text, using the With an icon, using an With text and an icon, Button class Image. Button class using Button class with the android: drawable. Left attribute Button <Button android: layout_width=" wrap_content" android: layout_height= "wrap_content" android: text="@string/ button_text" . . . /> Unit: 6 – Android Programming <Image. Button android: layout_width="wrap_content" android: layout_height="wrap_content" android: src="@drawable/button_icon" . . . /> 35 <Button android: layout_width="w rap_content" android: layout_height=" wrap_content" android: text="@string/bu tton_text" android: drawable. Left=" @drawable/button_icon" . . . /> Darshan Institute of Engineering & Technology

Radio. Button § Radio buttons allow the user to select one option from a set. § You should use radio buttons for optional sets that are mutually exclusive if you think that the user needs to see all available options side-by-side. Unit: 6 – Android Programming 36 Darshan Institute of Engineering & Technology

Radio. Button – Cont… § To create each radio button option, create a Radio. Button in your layout. § Radio button are mutually exclusive, so you must group them together inside a Radio. Group. § By grouping them together, the system ensures that only one radio button can be selected at a time. § Key classes are following: ✔ Radio. Button ✔ Radio. Group Unit: 6 – Android Programming <? xml version="1. 0" encoding="utf-8"? > <Radio. Group xmlns: android="http: //schemas. android. com/apk/res/android" android: layout_width="match_parent" android: layout_height="wrap_content" android: orientation="vertical"> <Radio. Button android: id="@+id/radio_pirates" android: layout_width="wrap_content" android: layout_height="wrap_content" android: text="@string/pirates" android: on. Click="on. Radio. Button. Clicked"/> <Radio. Button android: id="@+id/radio_ninjas" android: layout_width="wrap_content" android: layout_height="wrap_content" android: text="@string/ninjas" android: on. Click="on. Radio. Button. Clicked"/> </Radio. Group> 37 Darshan Institute of Engineering & Technology

Checkboxes § Checkboxes allow the user to select one or more options from a set. § Checkbox options allows the user to select multiple items. Your expertise? Android i. OS Unit: 6 – Android Programming Windows <? xml version="1. 0" encoding="utf-8"? > <Linear. Layout xmlns: android="http: //schemas. android. com/apk/res/android" android: orientation="vertical" android: layout_width="fill_parent" android: layout_height="fill_parent"> <Check. Box android: id="@+id/checkbox_meat" android: layout_width="wrap_content" android: layout_height="wrap_content" android: text="@string/meat" android: on. Click="on. Checkbox. Clicked"/> <Check. Box android: id="@+id/checkbox_cheese" android: layout_width="wrap_content" android: layout_height="wrap_content" android: text="@string/cheese" android: on. Click="on. Checkbox. Clicked"/> </Linear. Layout> 38 Darshan Institute of Engineering & Technology

Image. Button § Displays a button with an image that can be pressed or clicked by the user. § The image on the surface of the button is defined either by the android: src attribute in the <Image. Button> XML element or by the Image. View: set. Image. Resource(int) method. <Image. Button android: id="@+id/image. Button 1" android: layout_width="wrap_content" android: layout_height="wrap_content" android: src="@drawable/android_button" /> Unit: 6 – Android Programming 39 Darshan Institute of Engineering & Technology

Rating. Bar § A Rating. Bar is an extension of Seek. Bar and Progress. Bar that shows a rating in stars. § The user can touch/drag or use arrow keys to set the rating when using the default size Rating. Bar. Have you rated this app? * Unit: 6 – Android Programming <Rating. Bar android: id="@+id/rating. Bar" android: layout_width="wrap_content" android: layout_height=“wrap_content"/> 40 Darshan Institute of Engineering & Technology

Options Menus § The 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”. <? xml version="1. 0" encoding="utf-8"? > <menu xmlns: android="http: //schemas. android. com/apk/res/android"> <item android: id="@+id/file" android: title="@string/file" > <!-- "file" submenu --> <menu> <item android: id="@+id/create_new" android: title="@string/create_new" /> <item android: id="@+id/open" android: title="@string/open" /> </menu> </item> </menu> Unit: 6 – Android Programming 41 Darshan Institute of Engineering & Technology

Options Menus – Cont… § To specify the options menu for an activity, override on. Create. Options. Menu() § Here you can inflate your menu resource into the Menu provided in the callback. § When the user selects an item from the options menu, the system calls your activity’s on. Options. Item. Selected() method. § This method passes the Menu. Item selected. § You can identify the item by calling get. Item. Id(), which returns the unique ID for @Override the menu item public boolean on. Options. Item. Selected(Menu. Item item) { @Override public boolean on. Create. Options. Menu(Menu menu) { Menu. Inflater inflater = get. Menu. Inflater(); inflater. inflate(R. menu. game_menu, menu); return true; } Unit: 6 – Android Programming // Handle item selection switch (item. get. Item. Id()) { case R. id. new_game: new. Game(); return true; case R. id. help: show. Help(); return true; default: return super. on. Options. Item. Selected(item); } } 42 Darshan Institute of Engineering & Technology

Contextual Action Mode § A 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. Unit: 6 – Android Programming 43 Darshan Institute of Engineering & Technology

Context Menus § Implement the on. Create. Context. Menu() method in your Activity or Fragment. @Override public void on. Create. Context. Menu(Context. Menu menu, View v, Context. Menu. Info menu. Info) { super. on. Create. Context. Menu(menu, v, menu. Info); Menu. Inflater inflater = get. Menu. Inflater(); inflater. inflate(R. menu. context_menu, menu); } § When the user selects a menu item, the system calls this method so you can perform the appropriate action. @Override public boolean on. Context. Item. Selected(Menu. Item item) { Adapter. Context. Menu. Info info = (Adapter. Context. Menu. Info) item. get. Menu. Info(); switch (item. get. Item. Id()) { case R. id. edit: edit. Note(info. id); return true; case R. id. delete: delete. Note(info. id); return true; default: return super. on. Context. Item. Selected(item); } } Unit: 6 – Android Programming 44 Darshan Institute of Engineering & Technology

Thank You
- Slides: 45