Array of image button and textview in android -
i trying create array in each item have image button , below image button text view in center . can tell me how this. tried below code not getting worked
but=new imagebutton(this); but.setfocusableintouchmode(true); but.setid(1); imbrelp= new relativelayout.layoutparams(layoutparams.wrap_content,layoutparams.wrap_content); rel.addview(but,imbrelp); tv= new textview(this); tvrelp= new relativelayout.layoutparams(layoutparams.wrap_content,layoutparams.wrap_content); tvrelp.addrule(relativelayout.below, but.getid()); tvrelp.addrule(relativelayout.center_horizontal, but.getid()); rel.addview(tv, tvrelp); setcontentview(rel);
i avoid trying create relativelayout in code. it's going messy want do. instead, create xml layout file individual item, , inflate each item in array.
container layout in res/layout/main.xml:
<linearlayout android:id="@+id/container" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > </linearlayout>
item layout in res/layout/item.xml:
<relativelayout android:layout_width="match_parent" android:layout_height="wrap_content" > <imagebutton android:id="@+id/image_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/my_image" /> <textview android:id="@+id/text_view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/image_button" android:layout_centerhorizontal="true" android:text="@string/my_text" /> </relativelayout>
code in activity:
@override public void oncreate(bundle savedinstancestate) { setcontentview(r.layout.main); viewgroup container = (viewgroup) findviewbyid(r.layout.container); layoutinflater inflater = layoutinflater.from(this); item[] items = getmyarrayofitems(); (item : items) { view itemview = inflater.inflate(r.layout.item, container, false); imagebutton button = (imagebutton) itemview.findviewbyid(r.id.image_button); textview textview = (textview) itemview.findviewbyid(r.id.text_view); // todo set text , images container.addview(itemview); } }
Comments
Post a Comment