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

Similar documents
UI Fragment.

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

FRAGMENTS.

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

Mobile Computing Fragments

Fragments and the Maps API

ANDROID USER INTERFACE

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

Activities and Fragments

Building User Interface for Android Mobile Applications II

Fragments. Lecture 11

MODULE 2: GETTING STARTED WITH ANDROID PROGRAMMING

ACTIVITY, FRAGMENT, NAVIGATION. Roberto Beraldi

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

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

Mobile Development Lecture 10: Fragments

Fragments. Lecture 10

Fragments. Fragments may only be used as part of an ac5vity and cannot be instan5ated as standalone applica5on elements.

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

Creating a User Interface

LECTURE NOTES OF APPLICATION ACTIVITIES

Android User Interface

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

UNDERSTANDING ACTIVITIES

EMBEDDED SYSTEMS PROGRAMMING UI and Android

Android User Interface Android Smartphone Programming. Outline University of Freiburg

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

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

A view is a widget that has an appearance on screen. A view derives from the base class android.view.view.

Mobile Programming Lecture 2. Layouts, Widgets, Toasts, and Event Handling

Lecture 7: Data Persistence : shared preferences. Lecturer : Ali Kadhim Al-Bermani Mobile Fundamentals and Programming

Mobile User Interfaces

Overview of Activities

States of Activities. Active Pause Stop Inactive

ANDROID APP DEVELOPMENT

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

Activities. Repo:

Designing and Implementing Android UIs for Phones and Tablets

CS 528 Mobile and Ubiquitous Computing Lecture 3: Android UI, WebView, Android Activity Lifecycle Emmanuel Agu

EMBEDDED SYSTEMS PROGRAMMING Application Basics

ACTIVITY, FRAGMENT, NAVIGATION. Roberto Beraldi

Android Basics. Android UI Architecture. Android UI 1

CS 528 Mobile and Ubiquitous Computing Lecture 3b: Android Activity Lifecycle and Intents Emmanuel Agu

Android Application Development

CHAPTER 4. Fragments ActionBar Menus

This lecture. The BrowserIntent Example (cont d)

CS 4330/5390: Mobile Application Development Exam 1

Stanislav Rost CSAIL, MIT

Android development. Outline. Android Studio. Setting up Android Studio. 1. Set up Android Studio. Tiberiu Vilcu. 2.

University of Babylon - College of IT SW Dep. - Android Assist. Lect. Wadhah R. Baiee Activities

CS 4518 Mobile and Ubiquitous Computing Lecture 4: Data-Driven Views, Android Components & Android Activity Lifecycle Emmanuel Agu

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

User Interface: Layout. Asst. Prof. Dr. Kanda Runapongsa Saikaew Computer Engineering Khon Kaen University

Mobile Application Development Android

Android UI: Overview

ListView Containers. Resources. Creating a ListView

Lab 1: Getting Started With Android Programming

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

Real-Time Embedded Systems

Embedded Systems Programming - PA8001

CS378 - Mobile Computing. Anatomy of an Android App and the App Lifecycle

Programming with Android: System Architecture. Dipartimento di Scienze dell Informazione Università di Bologna

Android Basics. - Bhaumik Shukla Android Application STEALTH FLASH

More Effective Layouts

Introduction To Android

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

CE881: Mobile & Social Application Programming

Vienos veiklos būsena. Theory

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

04. Learn the basic widget. DKU-MUST Mobile ICT Education Center

Minds-on: Android. Session 2

(c) Dr Sonia Sail LAJMI College of Computer Sciences & IT (girl Section) 1

Programming with Android: System Architecture. Luca Bedogni. Dipartimento di Scienze dell Informazione Università di Bologna

Programmatically Working with Android Fragments

O X X X O O X O X. Tic-Tac-Toe. 5COSC005W MOBILE APPLICATION DEVELOPMENT Lecture 2: The Ultimate Tic-Tac-Toe Game

Initialising the Views (GameFragment) 5COSC005W MOBILE APPLICATION DEVELOPMENT Lecture 5: Ultimate Tic-Tac-Toe Game: The Interface (Part III)

Repairing crashes in Android Apps. Shin Hwei Tan Zhen Dong Xiang Gao Abhik Roychoudhury National University of Singapore

CMSC436: Fall 2013 Week 3 Lab

Using Eclipse for Android Development

Gauthier Picard. Ecole Nationale Supérieure des Mines

Android Programming: More User Interface. CS 3: Computer Programming in Java

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

Android Fundamentals - Part 1

Lifecycle-Aware Components Live Data ViewModel Room Library

The drawable/tile empty.xml file

CS 234/334 Lab 1: Android Jump Start

Lab 3. Accessing GSM Functions on an Android Smartphone

Introductory Android Development

14.1 Overview of Android

MC Android Programming

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

COSC 3P97 Mobile Computing

CS378 -Mobile Computing. User Interface Basics

Programming with Android: System Architecture. Dipartimento di Scienze dell Informazione Università di Bologna

Android UI DateBasics

Lifecycle Callbacks and Intents

Lecture 2 Android SDK

Android Programming (5 Days)

Android Activities. Akhilesh Tyagi

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

Transcription:

FRAGMENTS

Fragments An activity is a container for views When you have a larger screen device than a phone like a tablet it can look too simple to use phone interface here. Fragments Mini-activities, each with its own set of views One or more fragments can be embedded in an Activity You can do this dynamically as a function of the device type (tablet or not) or orientation ALSO, you can reuse fragments --- like reuse of mini interfaces

Fragments

Fragments Fragments were added to the Android API in Honeycomb, API 11. The primary classes related to fragments are: android.app.fragment The base class for all fragment definitions android.app.fragmentmanager The class for interacting with fragment objects inside an activity android.app.fragmenttransaction The class for performing an atomic set of fragment operations

FRAGMENTS A Fragment represents a behavior or a portion of user interface in an Activity. You can combine multiple fragments in a single activity to build a multi-pane UI and reuse a fragment in multiple activities. You can think of a fragment as a modular section of an activity, which has its own lifecycle, receives its own input events, and which you can add or remove while the activity is running (sort of like a "sub activity" that you can reuse in different activities). A fragment must always be embedded in an activity.

Fragment Lifecycle Fragment in an Activity---Activity Lifecycle influences Activity paused all its fragments paused Activity destroyed all its fragments are destroyed Activity running manipulate each fragment independently. Fragment transaction add, remove, etc. adds it to a back stack that's managed by the activity each back stack entry in the activity is a record of the fragment transaction that occurred. The back stack allows the user to reverse a fragment transaction (navigate backwards), by pressing the Back button.

Fragment Lifecycle

Fragment Lifecycle Several callback methods to handle various stages of a Fragment lifecycle: oncreate() called when creating the Fragment. oncreateview() called when UI for the Fragment is drawn for the first time. onpause() called when the user is leaving the Fragment.

Fragment inside Activity It lives in a ViewGroup inside the activity's view hierarchy Fragment has its own view layout. Using Fragments via XML: Insert a fragment into your activity layout by declaring the fragment in the activity's layout file, as a <fragment> element, via CODE: from your application code by adding it to an existing ViewGroup. You may also use a fragment without its own UI as an invisible worker for the activity.

Fragment methods (callback functions) onattach(activity) called once the fragment is associated with its activity. oncreate(bundle) called to do initial creation of the fragment. oncreateview(layoutinflater, ViewGroup, Bundle) creates and returns the view hierarchy associated with the fragment. onactivitycreated(bundle) tells the fragment that its activity has completed its own Activity.onCreaate. onstart() makes the fragment visible to the user (based on its containing activity being started). onresume() makes the fragment interacting with the user (based on its containing activity being resumed).

Fragment methods (callback functions) As a fragment is no longer being used, it goes through a reverse series of callbacks: onpause() fragment is no longer interacting with the user either because its activity is being paused or a fragment operation is modifying it in the activity. onstop() fragment is no longer visible to the user either because its activity is being stopped or a fragment operation is modifying it in the activity. ondestroyview() allows the fragment to clean up resources associated with its View. ondestroy() called to do final cleanup of the fragment's state. ondetach() called immediately prior to the fragment no longer being associated with its activity.

Fragments and their UI Most fragments will have a UI You must implement the oncreateview() callback method, which the Android system calls when it's time for the fragment to draw its layout. Your implementation of this method must return a View that is the root of your fragment's layout.

FRAGMENT SUB CLASSES There are three subclasses of fragments: DialogFragment : Displays a floating dialog ListFragment : Displays a list of items that are managed by an adapter PreferenceFragment : Displays a hierarchy of Preference objects as a list

Fragment Application Using Eclipse, create a new Android project and name it Fragments. In the res/layout folder, add a new file and name it fragment1.xml. Populate it with the following: <?xml version= 1.0 encoding= utf-8?> <LinearLayout xmlns:android= http://schemas.android.com/apk/res/android android:orientation= vertical android:layout_width= fill_parent android:layout_height= fill_parent android:background= #00FF00 >

Fragment Application <TextView android:layout_width= fill_parent android:layout_height= wrap_content android:text= This is fragment #1 android:textcolor= #000000 android:textsize= 25sp /> </LinearLayout>

Fragment Application Also in the res/layout folder, add another new file and name it fragment2.xml. Populate it as follows: <?xml version= 1.0 encoding= utf-8?> <LinearLayout xmlns:android= http://schemas.android.com/apk/res/android android:orientation= vertical android:layout_width= fill_parent android:layout_height= fill_parent android:background= #FFFE00 >

Fragment Application <TextView android:layout_width= fill_parent android:layout_height= wrap_content android:text= This is fragment #2 android:textcolor= #000000 android:textsize= 25sp /> </LinearLayout>

Fragment Application In main.xml, add the following code. <?xml version= 1.0 encoding= utf-8?> <LinearLayout xmlns:android= http://schemas.android.com/apk/res/android android:layout_width= fill_parent android:layout_height= fill_parent android:orientation= horizontal > <fragment android:name= com.example.fragments.fragment1 android:id= @+id/fragment1 android:layout_weight= 1 android:layout_width= 0px android:layout_height= match_parent />

Fragment Application <fragment android:name= com.example.fragments.fragment2 android:id= @+id/fragment2 android:layout_weight= 1 android:layout_width= 0px android:layout_height= match_parent /> </LinearLayout>

Fragment Application Add two Java class files and name them Fragment1.java and Fragment2.java public class Fragment1 extends Fragment { @Override public View oncreateview(layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) { } //---Inflate the layout for this fragment--- return inflater.inflate( R.layout.fragment1, container, false); }

Fragment Application public class Fragment2 extends Fragment { @Override public View oncreateview(layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) { } } //---Inflate the layout for this fragment--- return inflater.inflate( R.layout.fragment2, container, false);

Fragment Application return inflater.inflate( R.layout.fragment1, container, false)

Fragment Application

How It Works A fragment behaves very much like an activity it has a Java class and it loads its UI from an XML file. The Java class for a fragment needs to extend the Fragment base class. To draw the UI for a fragment, you override the oncreateview() method. This method needs to return a View object. Use a LayoutInflater object to inflate the UI from the specified XML file. The container argument refers to the parent ViewGroup, which is the activity in which you are trying to embed the fragment. The savedinstancestate argument enables you to restore the fragment to its previously saved state. Note that each fragment needs a unique identifier. You can assign one via the android:id or android:tag attribute.

Adding Fragments Dynamically Fragments enable to compartmentalize UI into various configurable parts. The real power of fragments is realized when you add them dynamically to activities during runtime. It is much more useful to create fragments and add them to activities during runtime. This enables you to create a customizable user interface for your application. For example, if the application is running on a smartphone, you might fill an activity with a single fragment; if the application is running on a tablet, you might then fill the activity with two or more fragments, as the tablet has much more screen real estate compared to a smartphone.

Adding Fragments Dynamically Using the same project created in the previous section, modify the main.xml file by commenting out the two <fragment> elements: <!-- <fragment android:name= com.example.fragments.fragment1.. android:layout_height= match_parent /> <fragment android:name= com.example.fragments.fragment2 android:layout_height= match_parent /> -->

Adding Fragments Dynamically Add the following code in bold to the FragmentsActivity.java file. public class FragmentsActivity extends Activity { public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); android.app.fragmentmanager fragmentmanager = getfragmentmanager(); FragmentTransaction fragmenttransaction = fragmentmanager.begintransaction(); WindowManager wm = getwindowmanager(); Display d = wm.getdefaultdisplay();

Adding Fragments Dynamically Point size = new Point(); d.getsize(size); int width = size.x; int height = size.y; if (width > height) { //---landscape mode--- Fragment1 fragment1 = new Fragment1(); // android.r.id.content refers to the content // view of the activity fragmenttransaction.replace(android.r.id.content, fragment1); }

Adding Fragments Dynamically else { //---portrait mode--- Fragment2 fragment2 = new Fragment2(); fragmenttransaction.replace(android.r.id.content, fragment2); } fragmenttransaction.commit(); } }

Adding Fragments Dynamically

THE END