Android Apps Development for Mobile and Tablet Device (Level I) Lesson 4. Workshop

Similar documents
1. Simple List. 1.1 Simple List using simple_list_item_1

Android Programs Day 5

Practical 1.ListView example

Android Apps Development for Mobile and Tablet Device (Level I) Lesson 2

Accelerating Information Technology Innovation

Q.1 Explain the dialog and also explain the Demonstrate working dialog in android.

Workshop. 1. Create a simple Intent (Page 1 2) Launch a Camera for Photo Taking

Meniu. Create a project:

Create Parent Activity and pass its information to Child Activity using Intents.

Produced by. Mobile Application Development. Higher Diploma in Science in Computer Science. Eamonn de Leastar

Tabel mysql. Kode di PHP. Config.php. Service.php

MAD ASSIGNMENT NO 2. Submitted by: Rehan Asghar BSSE AUGUST 25, SUBMITTED TO: SIR WAQAS ASGHAR Superior CS&IT Dept.

1. Location Services. 1.1 GPS Location. 1. Create the Android application with the following attributes. Application Name: MyLocation

05. RecyclerView and Styles

Mobile Programming Lecture 3. Resources, Selection, Activities, Intents

Mobile Programming Lecture 7. Dialogs, Menus, and SharedPreferences

Android Application Development. By : Shibaji Debnath

South Africa Version Control.

Action Bar. Action bar: Top navigation bar at each screen The action bar is split into four different functional areas that apply to most apps.

Fragment Example Create the following files and test the application on emulator or device.

Android Specifics. Jonathan Diehl (Informatik 10) Hendrik Thüs (Informatik 9)

Android Using Menus. Victor Matos Cleveland State University

ListView Containers. Resources. Creating a ListView

ActionBar. import android.support.v7.app.actionbaractivity; public class MyAppBarActivity extends ActionBarActivity { }

android:layout_margintop="5dp" > <EditText android:layout_width="210dp"

Mobila applikationer och trådlösa nät, HI1033, HT2013

Mobile Application Development Lab [] Simple Android Application for Native Calculator. To develop a Simple Android Application for Native Calculator.

Our First Android Application

Android Apps Development for Mobile Game Lesson Create a simple paint brush that allows user to draw something (Page 11 13)

Intents. Your first app assignment

TextView Control. EditText Control. TextView Attributes. android:id - This is the ID which uniquely identifies the control.

Introducing the Android Menu System

Programmation Mobile Android Master CCI

5Displaying Pictures and Menus with Views

POCKET STUDY. Divyam Kumar Mishra, Mrinmoy Kumar Das Saurav Singh, Prince Kumar

CS 4518 Mobile and Ubiquitous Computing Lecture 4: WebView (Part 2) Emmanuel Agu

Android Using Menus. Victor Matos Cleveland State University

ANDROID PROGRAMS DAY 3

<uses-permission android:name="android.permission.internet"/>

Produced by. Mobile Application Development. Higher Diploma in Science in Computer Science. Eamonn de Leastar

ITU- FAO- DOA- TRCSL. Training on. Innovation & Application Development for E- Agriculture. Shared Preferences

Mobila applikationer och trådlösa nät, HI1033, HT2012

Android Beginners Workshop

Developing Android Applications Introduction to Software Engineering Fall Updated 1st November 2015

CS 4518 Mobile and Ubiquitous Computing Lecture 3: Android UI Design in XML + Examples. Emmanuel Agu

MAD ASSIGNMENT NO 3. Submitted by: Rehan Asghar BSSE AUGUST 25, SUBMITTED TO: SIR WAQAS ASGHAR Superior CS&IT Dept.

CSE 660 Lab 3 Khoi Pham Thanh Ho April 19 th, 2015

Mobile Application Development MyRent Settings

Android Apps Development for Mobile Game Lesson 5

@Bind(R.id.input_ ) EditText EditText Button _loginbutton;

Action Bar. (c) 2010 Haim Michael. All Rights Reserv ed.

B9: Việc cuối cùng cần làm là viết lại Activity. Tới Example.java và chỉnh sửa theo nội dung sau: Mã: package at.exam;

EMBEDDED SYSTEMS PROGRAMMING UI Specification: Approaches

Preferences. Marco Ronchetti Università degli Studi di Trento

Creating a Custom ListView

Produced by. Mobile Application Development. Eamonn de Leastar

Building MyFirstApp Android Application Step by Step. Sang Shin Learn with Passion!

10.1 Introduction. Higher Level Processing. Word Recogniton Model. Text Output. Voice Signals. Spoken Words. Syntax, Semantics, Pragmatics

Adapter.

Android: Intents, Menus, Reflection, and ListViews

Open Lecture Mobile Programming. Intro to Material Design

Produced by. Design Patterns. MSc in Computer Science. Eamonn de Leastar

Tip Calculator. xmlns:tools=" android:layout_width="match_parent"

Have a development environment in 256 or 255 Be familiar with the application lifecycle

Basic GUI elements - exercises

MyDatabaseHelper. public static final String TABLE_NAME = "tbl_bio";

Android Hide Title Bar Example. Android Screen Orientation Example

COMP4521 EMBEDDED SYSTEMS SOFTWARE

Programming with Android: Animations, Menu, Toast and Dialogs. Luca Bedogni. Dipartimento di Informatica: Scienza e Ingegneria Università di Bologna

Group B: Assignment No 8. Title of Assignment: To verify the operating system name and version of Mobile devices.

Android Coding. Dr. J.P.E. Hodgson. August 23, Dr. J.P.E. Hodgson () Android Coding August 23, / 27


Time Picker trong Android

Developing Android Applications

Android Layout Types

1 카메라 1.1 제어절차 1.2 관련주요메서드 1.3 제작철차 서피스뷰를생성하고이를제어하는서피스홀더객체를참조해야함. 매니페스트에퍼미션을지정해야한다.


Produced by. Mobile Application Development. Higher Diploma in Science in Computer Science. Eamonn de Leastar

LifeStreet Media Android Publisher SDK Integration Guide

API Guide for Gesture Recognition Engine. Version 2.0

Android Navigation Drawer for Sliding Menu / Sidebar

ANDROID APPS DEVELOPMENT FOR MOBILE AND TABLET DEVICE (LEVEL I)

Android Tutorial: Part 3

Adaptation of materials: dr Tomasz Xięski. Based on presentations made available by Victor Matos, Cleveland State University.


EMBEDDED SYSTEMS PROGRAMMING Application Tip: Managing Screen Orientation

public AnimLayer(Context context, AttributeSet attrs, int defstyle) { super(context, attrs, defstyle); initlayer(); }

EMBEDDED SYSTEMS PROGRAMMING Application Tip: Saving State

TextView. A label is called a TextView. TextViews are typically used to display a caption TextViews are not editable, therefore they take no input

Chapter 7: Reveal! Displaying Pictures in a Gallery

Computer Science E-76 Building Mobile Applications

ListView (link) An ordered collection of selectable choices. key attributes in XML:

Mobile Computing Practice # 2c Android Applications - Interface

Starting Another Activity Preferences

Android/Java Lightning Tutorial JULY 30, 2018

M.A.D Assignment # 1

Produced by. Mobile Application Development. Higher Diploma in Science in Computer Science. Eamonn de Leastar

1. Explain the architecture of an Android OS. 10M The following diagram shows the architecture of an Android OS.

Java & Android. Java Fundamentals. Madis Pink 2016 Tartu

Database Development In Android Applications

Transcription:

Workshop 1. Create an Option Menu, and convert it into Action Bar (Page 1 8) Create an simple Option Menu Convert Option Menu into Action Bar Create Event Listener for Menu and Action Bar Add System Icon to Action Bar Add Custom Icon to Action Bar 2. Compare the simple list using different approach (Page 9 14) simple_list_item_1 simple_list_item_single_choice simple_list_item_checked simple_list_item_multiple_choice 3. Create the complicate list using advanced list technique (Page 15 20) Create List using ListActivity Switch on Text Filter Attach a Context Menu X4-XT-CDP-0251-A @ Peter Lo 2015

1. Action Bar 1.1 Menu and Submenu 1. Create the Android application using the following attributes. Application Name: MyActionBar Project Name: Package Name: MyActionBar com.example.myactionbar 2. Select the file "menu.xml" in the "res/menu" folder, then right click and select Open With Android Common XML Editor (or double click it to open). 3. In order to create new menu item, press the [Add] button. Then select Create a new element at the top level, in Menu and select Item in the dialog, press [OK] button to confirm. X4-XT-CDP-0251-A @ Peter Lo 2015 1

4. Set the Title to Menu Item 1 and save the menu. Repeat the steps to create Menu Item 2. 5. To create sub menu, press the [Add] button. Select Create a new element in the select element, item 2 (Item), and select Sub-Menu in the dialog. Press [OK] button to confirm. 6. To create sub menu item, press [Add] button again. Select Create a new element in the select element, item 2 (Item), and select Item in the dialog. Press [OK] button to confirm. X4-XT-CDP-0251-A @ Peter Lo 2015 2

7. Set the Title to Sub Item 3 and save the menu. Repeat the steps to create Sub Item 4 and Sub Item 5. 8. The XML code for the menu look like: <menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" tools:context="com.example.myactionbar.mainactivity" > <item android:id="@+id/action_settings" android:orderincategory="100" android:showasaction="never" android:title="@string/action_settings"/> <item android:id="@+id/item1" android:title="menu Item 1"/> <item android:id="@+id/item2" android:title="menu Item 2"> <menu> <item android:id="@+id/item3" android:title="sub Item 3"/> <item android:id="@+id/item4" android:title="sub Item 4"/> <item android:id="@+id/item5" android:title="sub Item 5"/> </menu> </item> </menu> 9. Save and execute the app, press the action bar and select Menu Item 2 to display the sub menu. X4-XT-CDP-0251-A @ Peter Lo 2015 3

1.2 Attach Menu into Action Bar 1. Select Menu Item 2, and then change the attribute of Show as action to always. 2. The XML for the menu become: <item android:id="@+id/item2" android:title="menu Item 2" android:showasaction="always"> <menu> <item android:id="@+id/item3" android:title="sub Item 3"/> <item android:id="@+id/item4" android:title="sub Item 4"/> <item android:id="@+id/item5" android:title="sub Item 5"/> </menu> </item> 3. Save and execute the app, can you find the different? X4-XT-CDP-0251-A @ Peter Lo 2015 4

1.3 Add System Icon to Action Bar 1. Set the icon for Sub Item 3 to @android:drawable/ic_menu_call. 2. The XML for the menu become: <item android:id="@+id/item2" android:title="menu Item 2" android:showasaction="always"> <menu> <item android:id="@+id/item3" android:title="sub Item 3" android:icon="@android:drawable/ic_menu_call"/> <item android:id="@+id/item4" android:title="sub Item 4"/> <item android:id="@+id/item5" android:title="sub Item 5"/> </menu> </item> 3. Save and execute the apps, press the Menu Item 2 to check the icon. X4-XT-CDP-0251-A @ Peter Lo 2015 5

1.4 Add Custom Icon to Action Bar 1. Drag the picture ic_menu_polyu.png into drawable-hdpi folder, select Copy Files in the File Operation dialog. 4. Set the icon for Sub Item 4 to @drawable/ic_menu_polyu. 5. The XML for the menu become: <item android:id="@+id/item2" android:title="menu Item 2" android:showasaction="always"> <menu> <item android:id="@+id/item3" android:title="sub Item 3" android:icon="@android:drawable/ic_menu_call"/> <item android:id="@+id/item4" android:title="sub Item 4" android:icon="@drawable/ic_menu_polyu"/> <item android:id="@+id/item5" android:title="sub Item 5"/> </menu> </item> 6. Save and execute the apps, can you see the icon in the menu? X4-XT-CDP-0251-A @ Peter Lo 2015 6

1.5 Handling Menu Event 1. Modify the source code for the file "MainActivity.java" as follow to handle menu event. package com.example.myactionbar; import android.app.activity; import android.os.bundle; import android.view.menu; import android.view.menuitem; import android.widget.toast; public class MainActivity extends Activity { @Override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); @Override public boolean oncreateoptionsmenu(menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getmenuinflater().inflate(r.menu.main, menu); return true; @Override public boolean onoptionsitemselected(menuitem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getitemid(); if (id == R.id.action_settings) { return true; // Handle click event Toast.makeText(this, item.tostring() + " selected", Toast.LENGTH_LONG).show(); return super.onoptionsitemselected(item); X4-XT-CDP-0251-A @ Peter Lo 2015 7

2. Save and execute the apps, press Menu button to call up the menu items. X4-XT-CDP-0251-A @ Peter Lo 2015 8

2. Simple List 2.1 Simple List using simple_list_item_1 1. Create the Android application with the following attributes. Application Name: MySimpleList Project Name: Package Name: MySimpleList com.example.mysimplelist 2. Remove the default text view Hello World 3. Right click the layout, and select Change Layout. 4. Change the layout to List View. X4-XT-CDP-0251-A @ Peter Lo 2015 9

5. The layout will be changed to: 6. Modify the source code for the file "MainActivity.java" as follow. package com.example.mysimplelist; import android.os.bundle; import android.app.activity; import android.view.menu; import android.view.view; import android.widget.adapterview; import android.widget.adapterview.onitemclicklistener; import android.widget.arrayadapter; import android.widget.listview; import android.widget.textview; import android.widget.toast; public class MainActivity extends Activity { private ListView ListView1; private String ListItems[] = { "Item 1", "Item 2", "Item 3", "Item 4"; ArrayAdapter<String> adapter; @Override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); // Define the visual style and set data for the list view X4-XT-CDP-0251-A @ Peter Lo 2015 10

ListView1 = (ListView)findViewById(R.id.ListView1); // Define a new Adapter adapter = new ArrayAdapter<String> (this,android.r.layout.simple_list_item_1, ListItems); // Assign adapter to ListView ListView1.setAdapter(adapter); // Register a callback to be invoked when an item in the list view clicked ListView1.setOnItemClickListener(new OnItemClickListener() { public void onitemclick(adapterview<?> parent, View view, int position, long id) { // Display a toast that show the content of the clicked list item Toast.makeText( getapplicationcontext(), ((TextView) view).gettext(), Toast.LENGTH_SHORT).show(); ); @Override public boolean oncreateoptionsmenu(menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getmenuinflater().inflate(r.menu.main, menu); return true; 7. Save and execute the app, then select the list item to observe the result. X4-XT-CDP-0251-A @ Peter Lo 2015 11

2.2 Simple List using simple_list_item_single_choice 1. Open the previous project and modify the source code for "MainActivity.java" as follow: protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); // Define the visual style and set data for the list view ListView1 = (ListView)findViewById(R.id.ListView1); // Define a new Adapter adapter = new ArrayAdapter<String> (this,android.r.layout.simple_list_item_single_choice, ListItems); // Define the selection mode Single choice ListView1.setChoiceMode(ListView.CHOICE_MODE_SINGLE); // Assign adapter to ListView ListView1.setAdapter(adapter); // Register a callback to be invoked when an item in the list view clicked ListView1.setOnItemClickListener(new OnItemClickListener() { public void onitemclick(adapterview<?> parent, View view, int position, long id) { // Display a toast that show the content of the clicked list item Toast.makeText( getapplicationcontext(), ((TextView) view).gettext(), Toast.LENGTH_SHORT).show(); ); 2. Save and execute the app, then select the list item to observe the result. X4-XT-CDP-0251-A @ Peter Lo 2015 12

2.3 Simple List using simple_list_item_checked 1. Open the previous project and modify the source code for "MainActivity.java" as follow: protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); // Define the visual style and set data for the list view ListView1 = (ListView)findViewById(R.id.ListView1); // Define a new Adapter adapter = new ArrayAdapter<String> (this,android.r.layout.simple_list_item_checked, ListItems); // Define the selection mode Multiple choice ListView1.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); // Assign adapter to ListView ListView1.setAdapter(adapter); // Register a callback to be invoked when an item in the list view clicked ListView1.setOnItemClickListener(new OnItemClickListener() { public void onitemclick(adapterview<?> parent, View view, int position, long id) { // Display a toast that show the content of the clicked list item Toast.makeText( getapplicationcontext(), ((TextView) view).gettext(), Toast.LENGTH_SHORT).show(); ); 2. Save and execute the app, then select the list item to observe the result. X4-XT-CDP-0251-A @ Peter Lo 2015 13

2.4 Simple List using simple_list_item_multiple_choice 1. Open the previous project and modify the source code for "MainActivity.java" as follow. protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); // Define the visual style and set data for the list view ListView1 = (ListView)findViewById(R.id.ListView1); // Define a new Adapter adapter = new ArrayAdapter<String> (this,android.r.layout.simple_list_item_multiple_choice, ListItems); // Define the selection mode Multiple choice ListView1.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); // Assign adapter to ListView ListView1.setAdapter(adapter); // Register a callback to be invoked when an item in the list view clicked ListView1.setOnItemClickListener(new OnItemClickListener() { public void onitemclick(adapterview<?> parent, View view, int position, long id) { // Display a toast that show the content of the clicked list item Toast.makeText( getapplicationcontext(), ((TextView) view).gettext(), Toast.LENGTH_SHORT).show(); ); 2. Save and execute the app, then select the list item to observe the result. X4-XT-CDP-0251-A @ Peter Lo 2015 14

3. Advanced List Technique 3.1 Create List using ListActivity 1. Create the Android application with the following attributes. Application Name: MyListActivity Project Name: Package Name: MyListActivity com.example.mylistactivity 2. Modify the source code for "MainActivity.java" as follow. package com.example.mylistactivity; import android.os.bundle; import android.app.activity; import android.view.menu; import android.app.listactivity; import android.view.view; import android.widget.arrayadapter; import android.widget.listview; import android.widget.textview; import android.widget.toast; public class MainActivity extends ListActivity { private String ListItems[] = { "Item 1", "Item 2", "Item 3", "Item 4"; private ArrayAdapter<String> adapter; @Override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); // setcontentview(r.layout.activity_main); // Define a new Adapter adapter = new ArrayAdapter<String> (this, android.r.layout.simple_list_item_1, ListItems); // Assign adapter to ListView setlistadapter(adapter); X4-XT-CDP-0251-A @ Peter Lo 2015 15

// Handle Item click event protected void onlistitemclick(listview list, View view, int position, long id){ Toast.makeText( getapplicationcontext(), ((TextView) view).gettext(), Toast.LENGTH_SHORT).show(); @Override public boolean oncreateoptionsmenu(menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getmenuinflater().inflate(r.menu.main, menu); return true; 3. Save and execute the app, then select the list item to observe the result. 3.2 Switch on Text Filter 1. Open the previous project and modify the source code for "MainActivity.java" as follow. protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); // setcontentview(r.layout.activity_main); // Define a new Adapter adapter = new ArrayAdapter<String> (this, android.r.layout.simple_list_item_1, ListItems); // Assign adapter to ListView setlistadapter(adapter); X4-XT-CDP-0251-A @ Peter Lo 2015 16

// Enables or disables the type filter window getlistview().settextfilterenabled(true); 2. Save and execute the app, then press 1 to observe the result. 3.3 Attach a Context Menu 1. Open the preview project and select the "res/menu" folder. Then right click and select New Android XML File. 2. Set the File as contextmenu and press [Finish] button. X4-XT-CDP-0251-A @ Peter Lo 2015 17

3. In order to create new context menu item, press the [Add] button. Then select Item in the dialog, press [OK] button to confirm. 4. Set the Title to Context Menu 1 and save the menu. Repeat the steps to create Context Menu 2. 5. The XML code for the content menu will look like: <?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android" > <item android:id="@+id/item1" android:title="context Menu 1"></item> <item android:id="@+id/item2" android:title="context Menu 2"></item> </menu> 6. Modify the source code for "MainActivity.java" as follow. package com.example.mylistactivity; import android.os.bundle; import android.app.activity; import android.view.menu; import android.app.listactivity; import android.view.view; import android.widget.arrayadapter; import android.widget.listview; X4-XT-CDP-0251-A @ Peter Lo 2015 18

import android.widget.textview; import android.widget.toast; import android.view.menuitem; import android.view.contextmenu; import android.view.contextmenu.contextmenuinfo; public class MainActivity extends ListActivity { private String ListItems[] = { "Item 1", "Item 2", "Item 3", "Item 4"; private ArrayAdapter<String> adapter; @Override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); // setcontentview(r.layout.activity_main); // Define a new Adapter adapter = new ArrayAdapter<String> (this, android.r.layout.simple_list_item_1, ListItems); // Assign adapter to ListView setlistadapter(adapter); // Enables or disables the type filter window getlistview().settextfilterenabled(true); // Handle Item click event protected void onlistitemclick(listview list, View view, int position, long id){ Toast.makeText( getapplicationcontext(), ((TextView) view).gettext(), Toast.LENGTH_SHORT).show(); // Register the content menu for the List item registerforcontextmenu(view); @Override public void oncreatecontextmenu(contextmenu contextmenu, View view, ContextMenuInfo menuinfo) { // Inflate the context menu X4-XT-CDP-0251-A @ Peter Lo 2015 19

super.oncreatecontextmenu(contextmenu, view, menuinfo); getmenuinflater().inflate(r.menu.contextmenu, contextmenu); public boolean oncontextitemselected (MenuItem item) { switch(item.getitemid()) { case R.id.item1: Toast.makeText(this, "Context Item 1 selected", Toast.LENGTH_LONG).show(); return true; case R.id.item2: Toast.makeText(this, "Context Item 2 selected", Toast.LENGTH_LONG).show(); return true; default: return super.oncontextitemselected(item); @Override public boolean oncreateoptionsmenu(menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getmenuinflater().inflate(r.menu.main, menu); return true; 7. Save and execute the app, then long click the list item to call up the context menu. X4-XT-CDP-0251-A @ Peter Lo 2015 20