Skip to content

Creating Dynamic Layouts

tsiougkosn edited this page Jan 28, 2015 · 1 revision

example 00

create and attach view in the specific Parent (created from xml)

/* load xml layout */
setContentView(R.layout.main);
ViewParent viewParent = (ViewParent) findViewById(R.id.parent_view_id)
JSONObject jsonObject = ... // load from network, sdcard etc
View sampleView = DynamicView.createView(this, jsonObject, viewParent);

example 01

create and attach view in the activity

JSONObject jsonObject = ... // load from network, sdcard etc
View sampleView = DynamicView.createView(this, jsonObject);
sampleView.setLayoutParams(
   new WindowManager.LayoutParams(
      WindowManager.LayoutParams.MATCH_PARENT,
      WindowManager.LayoutParams.MATCH_PARENT));
setContentView(sampleView);

example 02

create a view and get reference to specific children in the view tree

static public class SampleViewHolder {
   /* we need the View with id testClick */
   @DynamicViewId(id = "testClick")
   public View clickableView;

   /* Constructor must be public */
   public SampleViewHolder() {}
}

...

@Override
protected void onCreate(Bundle savedInstanceState) {
   /* load xml layout */
   setContentView(R.layout.main);
   ViewParent viewParent = (ViewParent) findViewById(R.id.parent_view_id)
   JSONObject jsonObject = ... // load from network, sdcard etc
   /* attach the view from the json in parent and add a tag in sampleView. */
   View sampleView = 
      DynamicView.createView(this, jsonObject, viewParent, SampleViewHolder.class);
/*
   The tag's class is SampleViewHolder. 
   The variables that Annotated @DynamicViewId will be the reference,
   to the actual view created, with id (that is the argument of the annotation).
   In this example the json contains a view with id "testClick"
   and the View return in clickableView
*/
   ((SampleViewHolder) sampleView.getTag())
      .clickableView.setOnClickListener( /* attach to click listener */ );

}