Android Application Model I

Similar documents
Android Application Model I. CSE 5236: Mobile Application Development Instructor: Adam C. Champion, Ph.D. Course Coordinator: Dr.

User Interface Development. CSE 5236: Mobile Application Development Instructor: Adam C. Champion Course Coordinator: Dr.

External Services. CSE 5236: Mobile Application Development Course Coordinator: Dr. Rajiv Ramnath Instructor: Adam C. Champion

Mobile Application Development MyRent Settings

Multiple devices. Use wrap_content and match_parent Use RelativeLayout/ConstraintLayout Use configuration qualifiers

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

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

CS371m - Mobile Computing. More UI Action Bar, Navigation, and Fragments

CS371m - Mobile Computing. More UI Navigation, Fragments, and App / Action Bars

Meniu. Create a project:

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

Mobile Computing Fragments

Fragments. Lecture 11

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

CS 4330/5390: Mobile Application Development Exam 1

Getting Started. Dr. Miguel A. Labrador Department of Computer Science & Engineering

Programming with Android: Android for Tablets. Dipartimento di Scienze dell Informazione Università di Bologna

Computer Science Large Practical: Storage, Settings and Layouts. Stephen Gilmore School of Informatics October 26, 2017

Agenda. The Android GUI Framework Introduction Anatomy A real word example Life cycle Findings

EMBEDDED SYSTEMS PROGRAMMING UI and Android

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

Solving an Android Threading Problem

Activities and Fragments

Mobile and Ubiquitous Computing: Android Programming (part 3)

Android Programs Day 5

07. Menu and Dialog Box. DKU-MUST Mobile ICT Education Center

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

Programming Android UI. J. Serrat Software Design December 2017

UI-Testing, Reactive Programming and some Kotlin.

ListView Containers. Resources. Creating a ListView

05. RecyclerView and Styles

Overview. What are layouts Creating and using layouts Common layouts and examples Layout parameters Types of views Event listeners

CS 528 Mobile and Ubiquitous Computing Lecture 4a: Fragments, Camera Emmanuel Agu

Mobile Programming Lecture 7. Dialogs, Menus, and SharedPreferences

Android. Applications Activities Interface

ANDROID USER INTERFACE

Android. Applications Activities Interface. Applications. Processes. Priorities. Activities

Java & Android. Java Fundamentals. Madis Pink 2016 Tartu

User Interface Design & Development

Diving into Android. By Jeroen Tietema. Jeroen Tietema,

Tablets have larger displays than phones do They can support multiple UI panes / user behaviors at the same time

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.

Mobile Development Lecture 10: Fragments

Android CardView Tutorial

Android Using Menus. Victor Matos Cleveland State University

CS378 -Mobile Computing. More UI -Part 2

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

Fragments. Lecture 10

EMBEDDED SYSTEMS PROGRAMMING Application Tip: Saving State

Android UI Development

EMBEDDED SYSTEMS PROGRAMMING Application Basics

Mobile Application Development Android

Mobile Programming Lecture 5. Composite Views, Activities, Intents and Filters

Android Using Menus. Victor Matos Cleveland State University

Tutorial: Setup for Android Development

Computer Science E-76 Building Mobile Applications

UI Fragment.

Embedded Systems Programming - PA8001

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

A Crash Course to Android Mobile Platform

Agenda. Overview of Xamarin and Xamarin.Android Xamarin.Android fundamentals Creating a detail screen

Understand applications and their components. activity service broadcast receiver content provider intent AndroidManifest.xml

Basic UI elements: Android Buttons (Basics) Marco Ronchetti Università degli Studi di Trento

Software Engineering Large Practical: Storage, Settings and Layouts. Stephen Gilmore School of Informatics October 27, 2017

Android Basics. Android UI Architecture. Android UI 1

Programming with Android: Layouts, Widgets and Events. Dipartimento di Scienze dell Informazione Università di Bologna

CS 4518 Mobile and Ubiquitous Computing Lecture 5: Rotating Device, Saving Data, Intents and Fragments Emmanuel Agu

Real-Time Embedded Systems

EMBEDDED SYSTEMS PROGRAMMING Application Tip: Switching UIs

Programming with Android: Introduction. Layouts. Dipartimento di Informatica: Scienza e Ingegneria Università di Bologna

Figure 2.10 demonstrates the creation of a new project named Chapter2 using the wizard.

Software Practice 3 Before we start Today s lecture Today s Task Team organization

Embedded Systems Programming - PA8001

Android UI: Overview

ANDROID APPS DEVELOPMENT FOR MOBILE GAME

Learn about Android Content Providers and SQLite

Android Workshop: Model View Controller ( MVC):

Fragments were added to the Android API in Honeycomb, API 11. The primary classes related to fragments are: android.app.fragment

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

Android App Development. Mr. Michaud ICE Programs Georgia Institute of Technology

Produced by. Mobile Application Development. David Drohan Department of Computing & Mathematics Waterford Institute of Technology

Android Basics. - Bhaumik Shukla Android Application STEALTH FLASH

UI, Continued. CS 2046 Mobile Application Development Fall Jeff Davidson CS 2046

Screen Slides. The Android Studio wizard adds a TextView to the fragment1.xml layout file and the necessary code to Fragment1.java.

getcount getitem getitemid getview com.taxi Class MainActivity drawerlayout drawerleft drawerright...

Building User Interface for Android Mobile Applications II

Simple Currency Converter

CSE 660 Lab 7. Submitted by: Arumugam Thendramil Pavai. 1)Simple Remote Calculator. Server is created using ServerSocket class of java. Server.

INTRODUCTION TO ANDROID

Android Services. Victor Matos Cleveland State University. Services

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

Android Tutorial: Part 3

ACTIVITY, FRAGMENT, NAVIGATION. Roberto Beraldi

Mobile Computing Practice # 2c Android Applications - Interface

More Effective Layouts

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

GUI Design for Android Applications

8/30/15 MOBILE COMPUTING. CSE 40814/60814 Fall How many of you. have implemented a command-line user interface?

Stanislav Rost CSAIL, MIT

CS260 Intro to Java & Android 05.Android UI(Part I)

Transcription:

Android Application Model I CSE 5236: Mobile Application Development Instructor: Adam C. Champion, Ph.D. Course Coordinator: Dr. Rajiv Ramnath Reading: Big Nerd Ranch Guide, Chapters 3, 5 (Activities); Chapter 13 (menus) 1

Android Framework Support 2

Framework Capabilities and Add-Ons Built-In Services: GUI OS services (file I/O, threads, device management) Graphics Device access (GPS, camera, media players, sensors), Networking Standard language libraries Add-ons: Google Play services (e.g. Google Maps, Games, etc.) Database support (SQLite) WebKit/Chromium 3

Tooling Support IDE: Android Studio Testing tools: JUnit, Espresso Performance profiling tools: Android Profiler Source code management: Git, Subversion, CVS, etc. Software emulators: Android emulator, Intel HAXM, Genymotion Sensor injection via emulator 4

Android Studio Project Components 5

Types of Android Programs Applications Home app, Home screen Take over screen Services Run in the background without UI App widgets and home screen widgets View-only interface to a service Home screen widget if on Home screen All apps have Activities a key part of any Android app 6

Activities in Tic-Tac-Toe 7

Specifying Activities AndroidManifest.xml <activity android:name=".splashscreen" android:label="@string/app_name" android:screenorientation="portrait"> <intent-filter> <action android:name="android.intent.action.main"/> <category android:name="android.intent.category.launcher /> </intent-filter> </activity> <activity android:name=".login" android:label="@string/app_name" android:launchmode="singleinstance" android:screenorientation="portrait"> <intent-filter> <action android:name="com.wiley.fordummies.androidsdk.login"/> <category android:name="android.intent.category.default"/> </intent-filter> </activity> 8

Implementing Activities Java public class LoginActivity extends SingleFragmentActivity {......... public class GameSessionActivity extends SingleFragmentActivity {...... Kotlin class LoginActivity : SingleFragmentActivity() {......... class GameSessionActivity : SingleFragmentActivity() {...... 9 Note: SingleFragmentActivity extends FragmentActivity

Activity UI Widgets View and ViewGroup Package android.view Specified declaratively in layout files Always use Fragments 10

Sample Layout: Login Activity <?xml version="1.0" encoding="utf-8"?> <ScrollView xmlns:android="http://schemas.android.com/apk/res/android android:background="@color/background" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="20dip"> <LinearLayout android:orientation="vertical <TextView android:text="@string/login_title /> <TextView /> <EditText /> <TextView /> <EditText /> <Button /> <Button /> <Button /> </LinearLayout> </ScrollView> 11 <project>/app/src/main/res/layout/fragment_login.xml

Views, ViewGroups, Layouts, Widgets Many types of views, layouts and widgets: ScrollView, HorizontalScrollView LinearLayout, AbsoluteLayout, FrameLayout, RelativeLayout TextView, EditText, Button, DatePicker, Spinner Nested nature of layout ViewGroup base class for composite elements View base class for terminal UI components Layout files compiled into resource R class in res subtree 12

Implement UI Logic: Listener Objects: Java public class LoginActivity extends SingleFragmentActivity { // Create LoginFragment in oncreate() method. (See SingleFragmentActivity) public class LoginFragment extends Fragment implements View.OnClickListener { private EditText musernameedittext; private EditText mpasswordedittext; @Override public View oncreateview() { // The real code has many null checks and screen rotation check (omitted for brevity) View v = inflater.inflate(r.layout.fragment_login, container, false); musernameedittext = v.findviewbyid(r.id.username_text); mpasswordedittext = v.findviewbyid(r.id.password_text); Button loginbutton = v.findviewbyid(r.id.login_button); loginbutton.setonclicklistener(this); Button cancelbutton = v.findviewbyid(r.id.cancel_button); cancelbutton.setonclicklistener(this); Button newuserbutton = v.findviewbyid(r.id.new_user_button); newuserbutton.setonclicklistener(this); return v; 13

Implement UI Logic: Listener Objects: Kotlin class LoginActivity : SingleFragmentActivity() { // Create LoginFragment in oncreate() method. (See SingleFragmentActivity) class LoginFragment : Fragment(), View.OnClickListener { private lateinit var musernameedittext: EditText private lateinit var mpasswordedittext: EditText override fun oncreateview( ): View? { // Similarly, real code has screen rotation check (omitted for brevity) val v = inflater.inflate(r.layout.fragment_login, container, false) musernameedittext = v.findviewbyid(r.id.username_text) mpasswordedittext = v.findviewbyid (R.id.password_text) val loginbutton = v.findviewbyid(r.id.login_button) loginbutton.setonclicklistener(this) val cancelbutton = v.findviewbyid(r.id.cancel_button) cancelbutton.setonclicklistener(this) val newuserbutton = v.findviewbyid(r.id.new_user_button) newuserbutton.setonclicklistener(this) return v // 14

The OnClick Handler Java public void onclick(view v) { switch (v.getid()) { case R.id.login_button: checklogin(); break; case R.id.cancel_button: finish(); break; case R.id.new_user_button: FragmentManager fm = getfragmentmanager(); Fragment fragment = new AccountFragment(); fm.begintransaction().replace(/* container */, fragment).addtobackstack("account_fragment").commit(); break; // Null check code omitted Kotlin override fun onclick(view: View) { when (view.id) { R.id.login_button -> checklogin() R.id.cancel_button -> activity?.finish() R.id.new_user_button -> val fm = fragmentmanager fm?.begintransaction()?.replace(/* container */, fragment)?.addtobackstack()?.commit() Kotlin s?. operator is short for: if (object!= null) { object.callmethod() 15

Embedding a View: GameSession Activity, Fragment: Java public class GameSessionActivity extends SingleFragmentActivity { public class GameSessionFragment extends Fragment { <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android.. > <LinearLayout... <com.wiley.fordummies.androidsdk.board android:id="@+id/board" android:layout_width="fill_parent" android:layout_height="fill_parent"/> </LinearLayout> <TextView... "/> <TextView... "/> </LinearLayout> 16

Embedding a View: GameSession Activity, Fragment: Kotlin class GameSessionActivity : SingleFragmentActivity() { class GameSessionFragment : Fragment() { <!-- Same XML as before --> 17

Embedding a View: Board Class Java // Board.java public class Board extends View {... public boolean ontouchevent( MotionEvent event) { switch (action) { case MotionEvent.ACTION_DOWN: break; return super.ontouchevent(event); Kotlin // Board.kt class Board : View { override fun ontouchevent( event: MotionEvent): Boolean { when (action) { MotionEvent.ACTION_DOWN -> { return super.ontouchevent(event) 18

Handling UI in the Activity Activity also a View Can handle UI without any widgets. Why? Handle non-widget-specific events (touch) Handle user interaction outside the boundaries of any UI components See ontouchevent method in SplashScreenFragment class. 19

Menus (Option menus) and Action Bars Declare the menu items Define oncreateoptionsmenu() and/or the oncreatecontextmenu() callback methods in Activity. Automatically called to create the menu (what if it doesn t exist?). Implement onoptionsitemselected() and/or oncontextitemselected() in activity. 20

Menu Layout File <?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:title="settings" android:id="@+id/menu_settings" android:icon="@android:drawable/ic_menu_preferences" /> <item android:title="help" /> android:id="@+id/menu_help" android:icon="@android:drawable/ic_menu_info_details" <item android:title="exit" android:id="@+id/menu_exit" android:icon="@android:drawable/ic_menu_close_clear_cancel" /> <item android:title="contacts" /> </menu> android:id="@+id/menu_contacts" android:icon="@android:drawable/ic_menu_view" Action Bar: Declare menu item with additional attribute 21 android:showasaction ="ifroom","never","withtext" or "always".

Menu Creation: Java public class GameOptionsFragment {... public View oncreateview( ) { // sethasoptionsmenu(true);... public boolean oncreateoptionsmenu(menu menu) { super.oncreateoptionsmenu(menu, inflater); inflater.inflate(r.menu.menu, menu);... 22

Menu Creation: Kotlin class GameOptionsFragment { override fun oncreateview( ) { // sethasoptionsmenu(true); override fun oncreateoptionsmenu( menu: Menu, inflater: MenuInflater) { super.oncreateoptionsmenu(menu, inflater) inflater.inflate(r.menu.menu_ingame, menu) 23

Menu Handlers: Java public boolean onoptionsitemselected(menuitem item) { Activity activity = getactivity(); if (activity!= null) { switch (item.getitemid()) { case R.id.menu_settings: startactivity(new Intent(activity, SettingsActivity.class)); return true; case R.id.menu_help: startactivity(new Intent(activity, HelpActivity.class)); return true; case R.id.menu_exit: showquitappdialog(); return true; case R.id.menu_contacts: startactivity(new Intent(activity, return false; ContactsActivity.class)); return true; 24

Menu Handlers: Kotlin override fun onoptionsitemselected(item: MenuItem?): Boolean { when (item!!.itemid) { R.id.menu_settings -> { startactivity(intent(activity?.applicationcontext, SettingsActivity::class.java)) return true R.id.menu_help -> { startactivity(intent(activity?.applicationcontext, HelpActivity::class.java)) return true R.id.menu_exit -> { showquitappdialog() return true R.id.menu_contacts -> { startactivity(intent(activity?.applicationcontext, ContactsActivity::class.java)) return true Kotlin s!! operator asserts that return false calling object is not null 25

UI for Larger Screens - Fragments In Android 3.0 and up with compatibility library (ACL) for earlier versions Further decouples UI interactions from activity lifecycle Standard concept in frameworks Allows reuse of UI components Specialized activity class FragmentActivity We will cover it in a later class 26

Special Types of Activities: Preferences: Java public class SettingsActivity extends AppCompatActivity { protected Fragment createfragment() { return new SettingsFragment(); @Override protected void oncreate(bundle savedinstancestate) { FragmentManager fm = getsupportfragmentmanager(); Fragment fragment = fm.findfragmentbyid(r.id.fragment_container); Fragment preferencefragment = createfragment(); fm.begintransaction().replace(r.id.fragment_container, preferencefragment).commit(); PreferenceManager.setDefaultValues(this, R.xml.settings, false); public class SettingsFragment extends PreferenceFragmentCompat { @Override public void oncreatepreferences(bundle savedinstancestate, String rootkey) { // Load preferences from XML resource. addpreferencesfromresource(r.xml.settings); 27

Special Types of Activities: Preferences: Kotlin class SettingsActivity : AppCompatActivity() { protected fun createfragment(): Fragment { return SettingsFragment() override fun oncreate(savedinstancestate: Bundle?) { val fm = supportfragmentmanager val fragment = fm.findfragmentbyid(r.id.fragment_container) val preferencefragment = createfragment() fm.begintransaction().replace(r.id.fragment_container, preferencefragment).commit() PreferenceManager.setDefaultValues(this, R.xml.settings, false) class SettingsFragment : PreferenceFragmentCompat() { override fun oncreatepreferences(savedinstancestate: Bundle?, rootkey: String?) { // Load preferences from XML resource. addpreferencesfromresource(r.xml.settings) 28

Layout File for a Preferences Activity <?xml version="1.0" encoding="utf-8"?> <PreferenceScreen xmlns:android=http://schemas.android.com/apk/res/android android:title="settings" android:background="@color/background"> <EditTextPreference android:key="name" android:title="player Info" android:summary="select your name" android:defaultvalue="player 1"/> <CheckBoxPreference android:key="human_starts" android:title="human Plays First" android:summary="check box to play first" android:defaultvalue="true" /> </PreferenceScreen> 29

Settings Java // Settings.java public class Settings { public static String getname( Context context) { return PreferenceManager.getDefaultSharedPreferences(context).getString(OPT_NAME, OPT_NAME_DEF); public static boolean doeshumanplayfirst( Context context) { return PreferenceManager.getDefaultSharedPreferences(context).getBoolean(OPT_PLAY_FIRST, OPT_PLAY_FIRST_DEF); Kotlin // Settings.kt object Settings { fun getname(context: Context): String { return PreferenceManager.getDefaultSharedPreferences(context).getString(OPT_NAME, OPT_NAME_DEF) fun doeshumanplayfirst(context: Context): Boolean { return PreferenceManager.getDefaultSharedPreferences(context).getBoolean(OPT_PLAY_FIRST, OPT_PLAY_FIRST_DEF) 30

Thank You Questions and comments? 31