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.

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.
Fragments: Introduction Fragments were introduced in Android 3.0 to support flexible and dynamic UI designs represent portions of an application’s user.
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.
Android Fragments.
Android Life Cycle CS328 Dick Steflik. Life Cycle The steps that an application goes through from starting to finishing Slightly different than normal.
Cosc 4730 Android TabActivity and ListView. TabActivity A TabActivity allows for multiple “tabs”. – Each Tab is it’s own activity and the “root” activity.
Android Development (Basics)
Programming with Android: Android Fragments Luca Bedogni Marco Di Felice Dipartimento di Scienze dell’Informazione Università di Bologna.
Android Application Development 2013 PClassic Chris Murphy 1.
Android UI, and Networking. Can do most networking on Android Bluetooth only on 2.0, Not supported with version 1.6.
Android Fragments A very brief introduction Android Fragments1.
CS378 - Mobile Computing Anatomy of an Android App and the App Lifecycle.
CS5103 Software Engineering Lecture 08 Android Development II.
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 5: Investigate! Lists, Arrays, and Web Browsers.
Tip Calculator App Building an Android App with Java © by Pearson Education, Inc. All Rights Reserved.
Android Boot Camp for Developers Using Java, Comprehensive: A Guide to Creating Your First Android Apps Chapter 5: Investigate! Android Lists, Arrays,
Cosc 5/4730 Introduction: Threads, Android Activities, and MVC.
Mobile Programming Lecture 6
Understand applications and their components activity service broadcast receiver content provider intent AndroidManifest.xml.
Android – Fragments L. Grewe.
© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Android Boot Camp.
Cosc 5/4730 Android Communications Intents, callbacks, and setters.
Mobile Programming Midterm Review
ListView and ExpandableListView
Android: “Dynamic” data and Preferences data.
More UI Action Bar, Navigation, and Fragments
User Interface Layout Interaction. EventsEvent Handlers/Listeners Interacting with a user.
© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Android Boot Camp.
Class on Fragments & threads. Fragments fragment is a modular section of an activity, which has its own lifecycle, receives its own input events, and.
CHAPTER 4 Fragments ActionBar Menus. Explore how to build applications that use an ActionBar and Fragments Understand the Fragment lifecycle Learn to.
Mobile Programming Lecture 4 Resources, Selection, Activities, Intents.
School of Engineering and Information and Communication Technology KIT305/KIT607 Mobile Application Development Android OS –Permissions (cont.), Fragments,
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.
Cosc 5/4730 Support design library. Support Design library Adds (API 9+) back support to a number of 5.0 lollipop widgets and material design pieces –
Chapter 5: Investigate! Lists, Arrays, and Web Browsers.
Fragments and Menus Chapter 4 1. Objectives Learn three different types of menus: options, context, and popup Learn to configure the ActionBar and Toolbar.
Lab7 – Appendix.
Introduction to android
Activity and Fragment.
Fragment ?.
Fragments: Introduction
Android 3: Fragments: API Guide
CS499 – Mobile Application Development
Mobile Application Development BSCS-7 Lecture # 6
Android 16: Fragments: Tutorial
Activities and Intents
Widgets & Fragments Kalin Kadiev Astea Solutions AD.
Mobile Application Development Chapter 4 [Android Navigation and Interface Design] IT448-Fall 2017 IT448- Fall2017.
Android 15: Fragments: API Guide
The Android Activity Lifecycle
Android – Fragments L. Grewe.
ANDROID UI – FRAGMENTS UNIT II.
Android Programming Lecture 6
CIS 470 Mobile App Development
Chapter 9: Fragments.
Android: Preferences and user settings data
Android Topics Custom ArrayAdapters
Activities and Intents
Android Topics Limited Resources and why we need to consider them.
CIS 470 Mobile App Development
Activities and Fragments
CS 240 – Advanced Programming Concepts
Android Development Tools
Activities, Fragments, and Intents
User Interface Development
Android Sensor Programming
Presentation transcript:

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 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). There is a the Support Library so your app remains compatible with devices running system versions as old as Android 1.6.

Fragments (2) We’ll start off very simple, using only a tablet size screen, so that our fragments will be shown Second we move onto supporting smaller size screens, where the both fragments don’t show at the same time We’ll start with native API 11+ and then come back to supporting older android devices

Basics The main activity is created as normal with a layout. The code the main activity is reduced to the onCreate and menu (?) – Otherwise, everything else is handled in the fragments. Fragments come in a couple of varieties like activities – Fragment, ListFragment (like a listView), DialogFragment, PreferenceFragment, and WebViewFragment

XML layout In the xml, we using fragment and associate each fragment with a java class of fragment Our example layout: <fragment android:layout_weight="1" android:layout_width="0px" android:layout_height="match_parent" class="edu.cs4755.frag1demo.titlefrag" /> <fragment android:layout_weight="2" android:layout_width="0px" android:layout_height="match_parent" class="edu.cs4755.frag1demo.textFrag" /> frag_titles uses titlefrag.java class

Fragment code In the Fragment, you have the same callback methods as an activity – onCreate(), onStart(), onPause(), and onStop() And OnCreateView() – The system calls this when it's time for the fragment to draw its user interface for the first time. To draw a UI for your fragment, you must return a View from this method that is the root of your fragment's layout. – You can return null if the fragment does not provide a UI.

Fragment code (2) You should implement at least these three OnCreate(), onCreateView() – The onCreate() is for setup of variables – OnCreateView() Android also says the OnPause(). See /fragments.html for the Fragment life cycle and the other callback methods. /fragments.html

Fragment The OnCreateView() returns a view and calls the layout associated with this fragment – R.layout.text is the layout being used for this public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.text, container, false); return view; } We also get a method getView(), so we can find and use the varying widgets into the layout. Example: – TextView tv = (TextView) getView().findViewById(R.id.text);

ListFragment Displays a list of items that are managed by an adapter (such as a SimpleCursorAdapter), similar to ListActivity. It provides several methods for managing a list view, such as the onListItemClick() callback to handle click events. It returns a ListView, instead of view and does not need to be implemented. Instead it can be done in the OnActivityCreate with setListAdapter()

ListFragment (2) public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); ArrayAdapter adapter = new ArrayAdapter (getActivity(), android.R.layout.simple_list_item_1, Shakespeare.TITLES); setListAdapter(adapter); }

Pulling it all together Source: Frag1demo.zip

Now supporting both sizes. In eclipse, create a res/layout-large directory Copy main.xml into the directory – This is now for “large” screens, like a tablet. Note, I would say create, but eclipse would only let me create a layout in the layout directory. Edit the res/layout/main.xml which will be our “small” screen device. – We are going to use a framelayout to hold the fragments.

res/layout/main.xml <FrameLayout xmlns:android=" k/res/android" android:layout_width="match_parent“ android:layout_height="match_parent"

In the activity In the first version, we had almost nothing in the activity, but now we need to load fragment (for small devices) In onCreate: // Check whether the activity is using the layout version with // the fragment_container FrameLayout. If so, we must add the first fragment if (findViewById(R.id.frag_container) != null ) { // However, if we're being restored from a previous state, // then we don't need to do anything and should return or else // we could end up with overlapping fragments. if (savedInstanceState != null) {return; } // Create an instance of ExampleFragment firstFragment = new titlefrag(); // In case this activity was started with special instructions from an Intent, // pass the Intent's extras to the fragment as arguments FirstFragment.setArguments(getIntent().getExtras()); // Add the fragment to the 'fragment_container' FrameLayout getFragmentManager().beginTransaction().add(R.id.frag_container, firstFragment).commit(); }

ListFragment (titlefrag) Now we need to put the text fragment on top when an item is clicked. textFrag frag = (textFrag) getFragmentManager().findFragmentById(R.id.frag_text); if (frag != null && frag.isInLayout()) { //layout-large frag.setText(position); } else { //smaller devices, it’s not showing, so we need to create it. frag = new textFrag(); Bundle args = new Bundle(); args.putInt("position", position); frag.setArguments(args); // Replace whatever is in the fragment_container view with this fragment, // and add the transaction to the back stack so the user can navigate back FragmentTransaction transaction = getFragmentManager().beginTransaction(); transaction.replace(R.id.frag_container, frag ); transaction.addToBackStack(null); //null is for the tag name, which we do not need. // Commit the transaction transaction.commit(); }

Other changes. Since the titlefrag has not been inflated yet, a bundle was sent to it with the position and in onCreate() for titlefrag, it loads the position out of the bundle to display text. See source code for other minor changes.

Both devices.

Issues. What I can not get working yet is two different layouts with fragments switching between landscape and portrait.

Support library for 2.X

Support library for 2.X (2) Now make sure that any you use fragment it is importing the correct fragment. import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager;... – Instead of android.app.Fragment Change the activity to a FragmentActivity And import android.support.v4.app.FragmentActivity;

Support library for 2.X (3) And now on a we get uses frag1demo as the base code File frag3demo.zip

Support library for 2.X (4) Make the same changes to frag2demo – Change getFragmentManager() to getSupportFragmentManger() Generally, the word Support will need to method calls. – A note, eclipse had a very hard time letting go of the v11+ api’s. I had to retype the getSupportFragmentManager() over again, so it would not give me an error. – Stupid eclipse…

Support library for 2.X (5) And we get

References See the frag1demo.zip and frag2demo.zip on the handout pages for source code. builder/get-started-with-android-fragments/ builder/get-started-with-android-fragments/ gments/creating.html gments/creating.html /fragments.html /fragments.html

Q A &