android - Creating a custom menu using a layout inflator, how do I reference each component? -
this item i'm inflating:
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="100dp" android:background="@drawable/background_border" android:padding="10dp" > <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="account name" android:textsize="25sp"/> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="balance: £61.43" android:textsize="25sp" android:paddingtop="40dp"/> <imageview android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_keyboard_arrow_right_black_24dp" android:layout_alignparentend="true" android:layout_centerinparent="true"/> </relativelayout>
and i'm inflating using following code:
linearlayout parent = (linearlayout) findviewbyid(r.id.linearl); view = getlayoutinflater().inflate(r.layout.menu_item, parent,false); parent.addview(view);
say if inflated following item 3 times, create 3 menu items - how reference each item individually?
for example, if wanted change text of account name in first item of menu, how reference it's in first item of menu, instead of second or third?
say if inflated following item 3 times, create 3 menu items - how reference each item individually?
use parent.getchildcount()
, parent.getchildat()
accessing views r.layout.menu_item
layout like:
add layout multiple times:
//first time parent.addview(view); //second time parent.addview(view);
get first added r.layout.menu_item
layout view:
view firstview=parent.getchildat(0);
in same way other child view of parent
layout.
access child views firstview
using :
view id if added in xml:
textview textview=(textview)firstview.findviewbyid(r.id.textviewid);
or using getchildat method:
textview textview=(textview)firstview.getchildat(0);
access other views in same way.
Comments
Post a Comment