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

While here is the research log...

Android custom components compound controls

Search for: Android custom components compound controls


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

Here are my notes on managing view state properly


BaseSavedState
dispatchSaveInstanceState
dispatchFreezeSelfOnly
SavedState
Parcelable

BaseSavedState dispatchSaveInstanceState dispatchFreezeSelfOnly

Search for: BaseSavedState dispatchSaveInstanceState dispatchFreezeSelfOnly


<?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>

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

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


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

By the argument above that will be wrong.

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

fragment dialogs from custom components

Search for: fragment dialogs from custom components

view getContext android

Search for: view getContext android

Here is some discussion

android dialogs in compound controls

Search for: android dialogs in compound controls

Here is that question on sof