Android Fragments for All

Posted on Mar 19, 2012 (2 years ago). Seen 3,954 times. 2 comments. Permalink Feed
Photo Max Walker
Director of Marketing
NewCircle, Inc.
Member since Sep 29, 2010
Location: San Francisco
Stream Posts: 295
Tagged as: Android Video

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.



Posted on Mar 20, 2012
Photo Marko Gargenta
Member since Jan 19, 2007
Location: San Francisco
Finally a concise explanation of how Fragments work. Btw, this was rated as top presentation at Android Open. Thanks Ken!
Posted on Mar 20, 2012
Photo Patrick Brown
Senior Software Engineer
Motorola Solutions, Inc.
Member since Aug 8, 2011
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.

application code and xml file


package com.motorola.mcust;

import java.util.Observable;
import java.util.Observer;

import android.os.Bundle;

import com.motorola.cust.CustomviewActivity;

public class MycustActivity extends Activity {
/** Called when the activity is first created. */
CustomviewActivity cubes;
public void onCreate(Bundle savedInstanceState) {

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(;

cubes.initialiseImages(i, 0, 0
, 50, 50, 50, 50);




<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=""
android:orientation="vertical" >



now custom control


package com.motorola.cust;


import android.content.Context;
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 );

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;

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);

this.getResources(), mImages[i]), null, mRect, null);
mX = mX + dX;
i += 1;
mX = mXInitial;

custom control main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=""
android:orientation="vertical" >

android:text="@string/hello" />