public class Myorders:activity {private DynamicControl _dynacontrol; Public Myorders () {_dynacontrol = new DynamicControl (); } protected override void OnCreate (bundle bundle) {base. OnCreate (bundle); try {setcontentview (Resource.Layout.MyOrders); var layout = (tablelayout) Findviewbyid (Resource.Id.myorders_layout); var header = AddHeader (); Layout. AddView (header); Layout. AddView (Addpendingserviceitem ("Service 1")); Layout. AddView (Addpendingserviceitem ("Service 2")); Layout. AddView (Addpendingserviceitem ("Service 3")); Layout. AddView (Addconfirmedserviceitem ("Service 4")); Layout. AddView (Addconfirmedserviceitem ("Service 5")); Layout. AddView (Addconfirmedserviceitem ("Service 6")); Layout. AddView (Addconfirmedserviceitem ("Service 7")); } catch (Exception ex) {this. Showalert (ex. Message); }} public View AddHeader () {var header = new TableRow (this); Header. Layoutparameters = new Tablelayout.layoutparams (TableLayout.LayoutParams.FillParent, TABLELAYOUT.LAYOUTPARAMS.W Rapcontent); Header. AddView (_dynacontrol.newtext (This, "Service")); Header. AddView (_dynacontrol.newtext (This, "Status")); Header. AddView (_dynacontrol.newtext (This, "Created at")); Header. AddView (_dynacontrol.newtext (This, "Action")); return header; Public View Addpendingserviceitem (string serviceName) {var tableRow = new TableRow (this); Tablerow.layoutparameters = new Tablelayout.layoutparams (TableLayout.LayoutParams.FillParent, Table Layout.LayoutParams.WrapContent); Tablerow.addview (_dynacontrol.newtext (this, string. Format("{0}", ServiceName)); Tablerow.addview (_dynacontrol.newtext (this, string. Format ("{0}", "Pending"), color.orangered)); Tablerow.addview (_dynacontrol.newtext (this, string. Format ("{0}", "2015-5-19 \r\n14:20:33")); Tablerow.addview (_dynacontrol.addbutton (This, "Details")); return tableRow; Public View Addconfirmedserviceitem (string serviceName) {var tableRow = new TableRow (this); Tablerow.addview (_dynacontrol.newtext (this, string. Format ("{0}", ServiceName)); Tablerow.addview (_dynacontrol.newtext (this, string. Format ("{0}", "confirmed"), Color.darkgreen)); Tablerow.addview (_dynacontrol.newtext (this, string. Format ("{0}", "2015-5-19 \ r \ n 14:30:43")); Tablerow.addview (_dynacontrol.addbutton (This, "rebook")); Tablerow.layoutparameters = new Tablelayout.layoutparams (TableLayout.LayoutParams.FillParent, TABLELAYOUT.L Ayoutparams.wrapcontENT); return tableRow; } }
Auxiliary classes:
public class DynamicControl {public Button AddButton (context context, string text, EventHandler clickaction = Nu ll) { var btn = new Button (context); Btn. SetText (text, TextView.BufferType.Normal); if (clickaction! = null) { btn. Click + = clickaction; } return btn; } Public TextView NewText (context context, string str, color? color = null) { var text = new TextView (context);
if (color. HasValue) { text. SetTextColor (color. Value); } Text. SetText (str, TextView.BufferType.Normal); return text; } }
Develop Android apps with C # + Xamarin-Add controls dynamically