Android Fragments for All

Android Fragments for All

Photo Max Walker
NewCircle, Inc.
Member since Sep 29, 2010
Stream Posts: 322
Posted on Mar 19, 2012 (3 years ago)
Seen 4617 times.
Tagged As:

In this video from Android Open 2011, Marakana's own Ken Jones is going to bring you up to speed on Fragments.

Android 3.0 introduced the Fragment class, intended to represent a behavior or portion of a user interface in an Activity. Since then, Google has released the Android Compatibility package to extend Fragment support to devices running Android 1.6 or later.

Fragments allow you to decompose the functionality and user interface of an application into reusable modules. You can then implement your Activities to assemble difference combinations of Fragments depending on the screen size and orientation of the device—for example, taking advantage of the large screen size of a tablet while still supporting smaller handheld devices.

However, Fragments provide benefits beyond improving your application's support for multiple screen sizes. For example, Fragments allow you to:

  • Simplify implementing tabbed interfaces and other dynamic interfaces
  • Provide intra-Activity Back button behavior that integrates with the existing Activity back stack
  • Easily retain state information across an Activity's runtime configuration change
  • Manage an Activity's background processing without a visible user interface component

 

Download Ken's slides here.

 

Comments

You have to login to post a comment.
Posted on Mar 20, 2012 (3 years ago)
Photo Patrick Brown
Senior Software Engineer
Motorola Solutions, Inc.
Member since Aug 8, 2011
[b]I am trying to get a custom control to work with a linearlayout and it crashes. The sytem does not properly load my R.layout.main when the custom control is started. The code works if the inflate is not involved in the custom control.However I want the main.xml file loaded as in the real application I have a lot of stuff. [/b] [b]application code and xml file[/b] [code] package com.motorola.mcust; import java.util.Observable; import java.util.Observer; import android.app.Activity; import android.os.Bundle; import com.motorola.cust.CustomviewActivity; public class MycustActivity extends Activity { /** Called when the activity is first created. */ CustomviewActivity cubes; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); int[] i; i = new int[7]; i[0] = R.drawable.ic_launcher; i[1] = R.drawable.ic_launcher; i[2] = R.drawable.ic_launcher; i[3] = R.drawable.ic_launcher; i[4] = R.drawable.ic_launcher; i[5] = R.drawable.ic_launcher; i[6] = R.drawable.ic_launcher; cubes = (CustomviewActivity) this.findViewById(R.id.cubes); cubes.initialiseImages(i, 0, 0 , 50, 50, 50, 50); } } [/code] [b]main.xml[/b] [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="vertical" > <com.motorola.cust.CustomviewActivity android:id="@+id/cubes" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center"/> </LinearLayout> [/code] [b]now custom control[/b] [code] package com.motorola.cust; import android.app.Activity; import android.content.Context; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.RectF; import android.util.AttributeSet; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.widget.LinearLayout; public class CustomviewActivity extends LinearLayout { public String TAG = "CustomviewActivity"; private int[] mImages; private int mX; private int dX; private int mY; private int dY; private int mXInitial; private RectF mRect; public CustomviewActivity(Context context, AttributeSet attrs) { super( context, attrs ); Log.v(TAG,"inflater"); LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); if(inflater != null){ Log.v(TAG,"inflater.inflate ing"); View view = inflater.inflate(com.motorola.cust.R.layout.main, this); Log.v(TAG,"inflater.inflate ed"); } } public void initialiseImages(int[] images, int topLeftX, int topLeftY, int imageWidth, int imageHeight, int viewX, int viewY) { mImages = images; dX = imageWidth; mX = topLeftX + viewX; dY = imageHeight; mY = topLeftY + viewY; mXInitial = mX; invalidate(); } @Override protected void onDraw(Canvas canvas) { int i = 0; int yTot; yTot = mY + dY; while (i < mImages.length) { mRect = new RectF(mX, mY, mX + dX,yTot); canvas.drawBitmap(BitmapFactory.decodeResource( this.getResources(), mImages[i]), null, mRect, null); mX = mX + dX; i += 1; } mX = mXInitial; } } [/code] [b]custom control main.xml[/b] [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="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> </LinearLayout> [/code]
Posted on Mar 20, 2012 (3 years ago)
Photo Marko Gargenta
@MarkoGargenta
Member since Jan 19, 2007
Finally a concise explanation of how Fragments work. Btw, this was rated as top presentation at [url=http://androidopen.com/android2011/public/schedule/detail/21003]Android Open[/url]. Thanks Ken!