Monday 15 August 2011

android - Generating View Dynamically -



android - Generating View Dynamically -

i got screen have static , dynamic views created. want infinity number of views created , think dynamic way forwards

now need create top column image buttons , edittext clientname , spinner cash in xml (this layout have currently) want if user clicks cart icon views product name, quantity , cost generated dynamically , bottom views generated via xml

like

as can see there're 2 row of product,quantity , price. done via xml can someboy help out how can generate , arrange row quantity , cost , product name in between existing xml layout this?

nb: main layout relative layout , layout contains row generated dynamically

<linearlayout android:id="@+id/laygroup0" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/lvpaymode" android:gravity="center" android:orientation="vertical" > <autocompletetextview android:id="@+id/autoproduct0" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_margin="5dp" android:drawableleft="@drawable/ic_shopping_cart_black_24dp" android:ems="10" android:hint="@string/strproductname" android:padding="10dp" android:singleline="true" /> <linearlayout android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:orientation="horizontal" > <edittext android:id="@+id/autoquantity0" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_margin="5dp" android:layout_weight="1" android:drawableleft="@drawable/ic_local_mall_black_24dp" android:ems="10" android:hint="@string/strquantity" android:inputtype="numberdecimal" android:padding="10dp" android:singleline="true" > </edittext> <edittext android:id="@+id/autoprice0" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_margin="5dp" android:layout_weight="1" android:drawableleft="@drawable/ic_attach_money_black_24dp" android:ems="10" android:hint="@string/strprice" android:inputtype="numberdecimal" android:padding="10dp" android:singleline="true" > </edittext> </linearlayout> </linearlayout>

i can not give finish code bc i'm writing phone, can give more pointers. create parent layout in xml in main layout. much easyer if linearlayout (any other layout much harder deal with, more care , more code). utilize findviewbyid reference parent in java code: linearlayout parent = findviewbyid(r.layout....); utilize parent add together in kid views.

next set button onclick event hooked up. in onclick event inflate kid layout: view kid = layoutinflater.from(context).inflate(r.layout.childxml);

next utilize textview tv = child.findviewbyid(... find views contained in etc. set other button events here if have buttons. @ lastly can add together kid parent: parent.addview(child);

if parent linear layout orientation set vertical views arrange nicely 1 below other. if add together many views risk extending more phone's screen allows , not scrollable. can overcome wrapping parent linear layout scrollview in xml. work nicely. hope helps.

android xml android-layout android-edittext dynamically-generated

No comments:

Post a Comment