Saturday, October 6, 2012

View and layoutparams

View Hierarchy

When creating views programmatically, there are rules to remember:

1. Each view has it's own layoutparams settings
2. layoutparams has different classes, depending on the parent of the view

For example, the layoutparams for a textview within a linearlayout and a textview with a relativelayout use different class - linearlayout.layoutparams and relativelayout.layoutparams.

Using incompatible layoutparams for views will crash the graphic creation engine.

Layoutparams are sometimes created automatically or are requires definition.

Example,

TextView tv = new TextView(context);
CurrentLayout.add (tv); //adds view and automatically assign currentlayout layoutparams into tv

or

TextView tv = new TextView(context);
LinearLayout.LayoutParams lparams = new LinearLayout.LayoutParams(100,100); //set width and height to 100
tv.setLayoutParams( lparams);
CurrentLayout.add (tv); //Current Layout MUST be a linearlayout or an exception will be raise