All of this research is now collected into a semi article here.

While here is the research log...

satya - Tue Oct 30 2012 09:21:25 GMT-0400 (Eastern Daylight Time)

Android custom components compound controls

Android custom components compound controls

Search for: Android custom components compound controls

satya - Tue Oct 30 2012 09:32:33 GMT-0400 (Eastern Daylight Time)

Couple of high level approaches


Inherit from existing view like TextView and something special
Inherit from a layout like LinearLayout and something special
  Use inline layouts
  Use merge in layouts
Manage state to allow for duplicate ids

satya - Tue Oct 30 2012 10:18:43 GMT-0400 (Eastern Daylight Time)

Here are my notes on managing view state properly

Here are my notes on managing view state properly

satya - Tue Oct 30 2012 10:20:16 GMT-0400 (Eastern Daylight Time)

Key words to understand this problem


BaseSavedState
dispatchSaveInstanceState
dispatchFreezeSelfOnly
SavedState
Parcelable

satya - Tue Oct 30 2012 10:20:34 GMT-0400 (Eastern Daylight Time)

BaseSavedState dispatchSaveInstanceState dispatchFreezeSelfOnly

BaseSavedState dispatchSaveInstanceState dispatchFreezeSelfOnly

Search for: BaseSavedState dispatchSaveInstanceState dispatchFreezeSelfOnly

satya - Tue Oct 30 2012 14:16:06 GMT-0400 (Eastern Daylight Time)

Here is an example of a merge file


<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<TextView  
   android:id="@+id/text1"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Debut Text Appears here"
    />
<Button  
   android:id="@+id/button1"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Click me"
    android:onClick="buttonClicked"
    />
</merge>

satya - Tue Oct 30 2012 14:16:31 GMT-0400 (Eastern Daylight Time)

Here is how this layout is loaded


public class DurationControl 
extends LinearLayout
{
   public DurationControl(Context context) {
      super(context);
      // TODO Auto-generated constructor stub
      initialize(context);
   }

   public DurationControl(Context context, AttributeSet attrs, int defStyle) {
      super(context, attrs, defStyle);
      // TODO Auto-generated constructor stub
      initialize(context);
   }

   public DurationControl(Context context, AttributeSet attrs) {
      super(context, attrs);
      // TODO Auto-generated constructor stub
      initialize(context);
   }
   private void initialize(Context context)
   {
      LayoutInflater lif = (LayoutInflater) 
      context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        lif.inflate(R.layout.duration_view_layout, this);      
   }
}

satya - Tue Oct 30 2012 14:19:54 GMT-0400 (Eastern Daylight Time)

The meaning of this line


inflate(R.layout.duration_view_layout, this)

Read the layout and attach the resulting object or objects (in case of merge) to the node that is "this". In our case "this" is the linear layout itself.

if we had not used "merge" and used "LinearLayout" as the starting point then we would have had a linear layout inside a linear layout.

Because "merge" is a "pure" attach instruction all attributes that belong to this linear layout will go where we specify the custom control. Below is an example

satya - Tue Oct 30 2012 14:21:01 GMT-0400 (Eastern Daylight Time)

Why merge node in the xml has no attributes of its own because they belong to the parent


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:apptemplate="http://schemas.android.com/apk/res/com.androidbook.compoundControls"    
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    >
<TextView  
   android:id="@+id/text2"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Custom Hello"
    />

<com.androidbook.compoundControls.DurationControl
   android:id="@+id/durationControlId"
    android:orientation="vertical"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    /> 

</LinearLayout>

satya - Tue Oct 30 2012 14:21:47 GMT-0400 (Eastern Daylight Time)

I was initially tempted to put the orientation attribute under merge

By the argument above that will be wrong.

satya - Tue Oct 30 2012 14:56:22 GMT-0400 (Eastern Daylight Time)

How can I get a fragment manager from a view in android

How can I get a fragment manager from a view in android

Search for: How can I get a fragment manager from a view in android

satya - Tue Oct 30 2012 15:08:57 GMT-0400 (Eastern Daylight Time)

fragment dialogs from custom components

fragment dialogs from custom components

Search for: fragment dialogs from custom components

satya - Tue Oct 30 2012 15:11:24 GMT-0400 (Eastern Daylight Time)

view getContext android

view getContext android

Search for: view getContext android

satya - Tue Oct 30 2012 15:15:33 GMT-0400 (Eastern Daylight Time)

Here is some discussion

Here is some discussion

satya - Tue Oct 30 2012 15:16:21 GMT-0400 (Eastern Daylight Time)

android dialogs in compound controls

android dialogs in compound controls

Search for: android dialogs in compound controls

satya - Tue Oct 30 2012 15:18:19 GMT-0400 (Eastern Daylight Time)

Here is that question on sof

Here is that question on sof