School of Engineering and Information and Communication Technology KIT305/KIT607 Mobile Application Development Android OS –Permissions (cont.), Fragments,

Slides:



Advertisements
Similar presentations
Programming with Android: Android for Tablets Luca Bedogni Marco Di Felice Dipartimento di Scienze dellInformazione Università di Bologna.
Advertisements

Programming with Android: Android for Tablets Luca Bedogni Marco Di Felice Dipartimento di Scienze dell’Informazione Università di Bologna.
Android architecture overview
Fragments: Introduction Fragments were introduced in Android 3.0 to support flexible and dynamic UI designs represent portions of an application’s user.
Programming with Android: Activities
Android 02: Activities David Meredith
All About Android Introduction to Android 1. Creating a New App “These aren’t the droids we’re looking for.” Obi-wan Kenobi 1. Bring up Eclipse. 2. Click.
The Android Activity Lifecycle. Slide 2 Introduction Working with the Android logging system Rotation and multiple layouts Understanding the Android activity.
The Activity Class 1.  One application component type  Provides a visual interface for a single screen  Typically supports one thing a user can do,
Application Fundamentals. See: developer.android.com/guide/developing/building/index.html.
Android Fragments.
1 CSCE 4013: Mobile Systems Programming Nilanjan Banerjee Mobile Systems Programming University of Arkansas Fayetteville, AR
More on User Interface Android Applications. Layouts Linear Layout Relative Layout Table Layout Grid View Tab Layout List View.
Android Development (Basics)
Programming with Android: Android Fragments Luca Bedogni Marco Di Felice Dipartimento di Scienze dell’Informazione Università di Bologna.
Creating Android user interfaces using layouts 1Android user interfaces using layouts.
Chien-Chung Shen Manifest and Activity Chien-Chung Shen
CS378 - Mobile Computing Anatomy of an Android App and the App Lifecycle.
ANDROID UI – FRAGMENTS. Fragment  An activity is a container for views  When you have a larger screen device than a phone –like a tablet it can look.
Chapter 2: Simplify! The Android User Interface
Cosc 5/4730 Introduction: Threads, Android Activities, and MVC.
This work is licensed under the Creative Commons Attribution 4.0 International License. To view a copy of this license, visit
Mobile Programming Lecture 6
DUE Hello World on the Android Platform.
Copyright© Jeffrey Jongko, Ateneo de Manila University Of Activities, Intents and Applications.
Chapter 2 The Android User Interface. Objectives  In this chapter, you learn to:  Develop a user interface using the TextView, ImageView, and Button.
Understand applications and their components activity service broadcast receiver content provider intent AndroidManifest.xml.
Android – Fragments L. Grewe.
Android Boot Camp for Developers Using Java, Comprehensive: A Guide to Creating Your First Android Apps Chapter 2: Simplify! The Android User Interface.
Creating an Example Android App in Android Studio Activity lifecycle & UI Resources.
Cosc 4730 Android Fragments. Fragments You can think of a fragment as a modular section of an activity, which has its own lifecycle, receives its own.
CS378 - Mobile Computing User Interface Basics. User Interface Elements View – Control – ViewGroup Layout Widget (Compound Control) Many pre built Views.
Class on Fragments & threads. Fragments fragment is a modular section of an activity, which has its own lifecycle, receives its own input events, and.
Activities and Intents Chapter 3 1. Objectives Explore an activity’s lifecycle Learn about saving and restoring an activity Understand intents and how.
CHAPTER 4 Fragments ActionBar Menus. Explore how to build applications that use an ActionBar and Fragments Understand the Fragment lifecycle Learn to.
ANDROID LAYOUTS AND WIDGETS. Slide 2 Introduction Parts of the Android screen Sizing widgets and fonts Layouts and their characteristics Buttons, checkboxes.
The Ingredients of Android Applications. A simple application in a process In a classical programming environment, the OS would load the program code.
Android Fragments. Slide 2 Lecture Overview Getting resources and configuration information Conceptualizing the Back Stack Introduction to fragments.
The Flag Quiz app tests your ability to correctly identify 10 flags from various countries and territories.
Working with Multiple Activities. Slide 2 Introduction Working with multiple activities Putting together the AndroidManifest.xml file Creating multiple.
CHAPTER 1 part 1 Introduction. Chapter objectives: Understand Android Learn the differences between Java and Android Java Examine the Android project.
Fragments and Menus Chapter 4 1. Objectives Learn three different types of menus: options, context, and popup Learn to configure the ActionBar and Toolbar.
Introduction to android
Android Application -Architecture.
Permissions.
Activity and Fragment.
GUI Programming Fundamentals
Activities, Fragments, and Events
CS499 – Mobile Application Development
Mobile Application Development BSCS-7 Lecture # 6
Mobile Applications (Android Programming)
Activities and Intents
Mobile Application Development Chapter 4 [Android Navigation and Interface Design] IT448-Fall 2017 IT448- Fall2017.
The Android Activity Lifecycle
Sensors, maps and fragments:
Android – Fragments L. Grewe.
ANDROID UI – FRAGMENTS UNIT II.
CIS 470 Mobile App Development
CIS 470 Mobile App Development
Activities and Intents
Activities and Intents
HNDIT2417 Mobile Application Development
CIS 470 Mobile App Development
CIS 470 Mobile App Development
Mobile Programming Dr. Mohsin Ali Memon.
Activities and Fragments
Activities, Fragments, and Intents
Android Sensor Programming
CIS 694/EEC 693 Android Sensor Programming
Presentation transcript:

School of Engineering and Information and Communication Technology KIT305/KIT607 Mobile Application Development Android OS –Permissions (cont.), Fragments, Saving State, and Material Design Dr. Rainer Wasinger, Dr. James Montgomery School of Engineering and ICT

School of Engineering and Information and Communication Technology System Permissions (cont.) If your app needs a dangerous permission, you must check whether you have that permission every time you perform an operation that requires that permission. Example: To request dangerous permissions, we need to call additional methods like: –requestPermissions(). 2 int permissionCheck = ContextCompat.checkSelfPermission(thisActivity, Manifest.permission.WRITE_CALENDAR);

School of Engineering and Information and Communication Technology System Permissions (cont.) To request dangerous permissions, we need to call the method: –requestPermissions(). 3 if (ContextCompat.checkSelfPermission(thisActivity, Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) { if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity, Manifest.permission.READ_CONTACTS)) { } else { ActivityCompat.requestPermissions(thisActivity, new String[]{Manifest.permission.READ_CONTACTS}, MY_PERMISSIONS_REQUEST_READ_CONTACTS); }

School of Engineering and Information and Communication Technology System Permissions (cont.) When the user responds to your request, the system invokes the onRequestPermissionsResult() callback. public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) { switch (requestCode) { case MY_PERMISSIONS_REQUEST_READ_CONTACTS: { // If request is cancelled, the result arrays are empty. if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { // permission was granted, so do the task you need to do. } else { // permission denied, so disable the // functionality that depends on this permission. } return; } // other 'case' lines to check for other permissions }

School of Engineering and Information and Communication Technology Designing for multiple form factors Android 3.0 introduced Android for Tablets –Android tablet screens typically range from 7” to 12” diagonally. –Similar to responsive web design, one needs to consider how the app should be presented based on different available screen widths. –The goal is to design your app once and optimize the design for tablets and older versions of Android (i.e. not separate apps). 5

School of Engineering and Information and Communication Technology Fragments Fragments are an optional layer that can be put between activities and widgets to help reconfigure the app to support both large screens (e.g. tablets) and small screens (e.g. phones). –They were first introduced in Android 3.0, but can support devices down to Android 1.6 through the backward compatibility support libraries. –By dividing the layout of an activity into fragments, we can modify the activity’s appearance at runtime. 6

School of Engineering and Information and Communication Technology Fragments Here is an example showing how two UI modules can be combined into one activity on a tablet, but split into two activities for a phone. 7

School of Engineering and Information and Communication Technology Fragments Steps required to implement Fragments: 1.Create a Fragment class 2.For large displays: Add a Fragment to an Activity in XML. 3.For small displays: Add a Fragment to an Activity in code at Runtime. Replace Fragments with other Fragments as needed. 8

School of Engineering and Information and Communication Technology Fragments Example – Phone and Tablet UI Phone Tablet 9

School of Engineering and Information and Communication Technology Fragments Example – Source Files 10 AndroidManifest.xml build.grade (Module: app) MainActivity.java HeadlinesFragment.java ArticleFragment.java Ipsum.java \res\layout\news _articles.xml \res\layout- large\news_articl es.xml \res\layout\article _view.xml

School of Engineering and Information and Communication Technology Step 1: Create a Fragment class To create a fragment, we create a subclass of Fragment, and implement callback methods like onCreate(), onCreateView(), and onPause(). –Callback methods for Fragments are similar to Activities, but onCreateView() is different. –onCreateView() is used to define the layout. 11

School of Engineering and Information and Communication Technology public class ArticleFragment extends Fragment public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {... return inflater.inflate(R.layout.article_view, container, false); } Step 1: Create a Fragment class 12 ArticleFragment.java \res\layout\article _view.xml

School of Engineering and Information and Communication Technology Step 2: For Large Displays For large displays, we add a Fragment to an Activity in XML. 13 public class MainActivity extends FragmentActivity implements HeadlinesFragment.OnHeadlineSelectedListener public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.news_articles); MainActivity.java \res\layout- large\news_articl es.xml

School of Engineering and Information and Communication Technology Step 3: For Small Displays For small displays, we add and replace Fragments in code at Runtime. 14 public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.news_articles); if (findViewById(R.id.fragment_container) != null) { if (savedInstanceState != null) { return; } HeadlinesFragment firstFragment = new HeadlinesFragment(); firstFragment.setArguments(getIntent().getExtras()); getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, firstFragment).commit(); } } \res\layout\news _articles.xml public void onArticleSelected(int position) {... FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); transaction.replace(R.id.fragment_container, newFragment); transaction.addToBackStack(null); transaction.commit(); } } MainActivity.java

School of Engineering and Information and Communication Technology Saving State An activity can be destroyed by either: –A system constraint, or E.g. when your activity is currently stopped and hasn’t been used for a long time, or when the current foreground activity requires more resources (in which case the system shuts down background processes to recover memory). –As part of the normal app behaviour. E.g. the user presses the ‘Back’ button, or the activity signals its own destruction by calling finish(). If the system destroys the activity due to a system constraint, when the user navigates back to it, the system will need to create a new instance using a set of saved data that describes the state of the activity when it was destroyed. 15

School of Engineering and Information and Communication Technology Saving State Your activity is also destroyed and recreated each time the user rotates the screen. –Information in Widgets (TextView, Buttons, etc.) will be persisted automatically by Android as long as you assign an ID to them. So that means most of the UI state is taken care of without issue. Only when you need to store other data does this become an issue. -This is done because your activity might need to load alternative resources to accommodate the change in rotation. 16

School of Engineering and Information and Communication Technology static final String BUTTON_PRESSES = "buttonPresses"; int counter = public void onSaveInstanceState(Bundle savedInstanceState) { // Save the user's current game state savedInstanceState.putInt(BUTTON_PRESSES, counter); // Always call the superclass so it can save the view hierarchy state super.onSaveInstanceState(savedInstanceState); } Saving State To save your activity state: 17

School of Engineering and Information and Communication Technology Saving State To restore your activity state: protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Check whether we're recreating a previously destroyed instance if (savedInstanceState != null) { // Restore value of members from saved state counter = savedInstanceState.getInt(BUTTON_PRESSES); }... }

School of Engineering and Information and Communication Technology Material Design What is it? –“A new approach to cross-product, cross-platform design that uses tangible surfaces, bold graphic design, and meaningful motion to help define the way software should behave”. –An interface that incorporates “tactile surfaces, bold graphic design, and fluid motion to create beautiful, intuitive experiences”. It’s goal: –To develop a single underlying system that allows for a unified experience across platforms and device sizes. –It is the default visual language for Android 5.0+ devices. 19

School of Engineering and Information and Communication Technology Material Design - Demo 20

School of Engineering and Information and Communication Technology Material Design – Differences to iOS Android uses DP units, while iOS uses points. –1dp = (or 160dpi). On Android, apps can integrate deeply with other apps, removing the need to re-implement functionality (e.g. taking photos). Android provides a system Back button, in addition to Home and Overview for task switching. Android tabs should be placed at the top of the screen rather than the bottom with iOS. Long-presses should only be used to select an item, begin dragging it, or to do nothing. 21

School of Engineering and Information and Communication Technology Material Design – Density-independent pixels Density-independent pixels (DP): –1dp = (where 1x is 160dpi). Density buckets: 22 Density bucket Scaling factor Density MDPI1x160dpi HDPI1.5x240dpi XHDPI2x320dpi XXHDPI3x480dpi XXXHDPI4x640dpi

School of Engineering and Information and Communication Technology Material Design – Material in a 3D space The material environment is a 3D space Material has varying x and y dimensions (measured in dp), but a uniform thickness (1dp). 23