android - Assigning unique ids to descendants of included layouts -
i have following layouts setup (i omitted irrelevant tags):
main.xml: <include android:id="@+id/test1" layout="@layout/mylayout" /> <include android:id="@+id/test2" layout="@layout/mylayout" /> mylayout.xml: <linearlayout <edittext android:id="@+id/myedittext" /> </linearlayout>
note particular example crucially oversimplified, actual mylayout.xml
complex , contains lot of sublayouts , subviews lot of ids , really not inline main.xml
.
so, want reuse layout mylayout.xml
multiple times. might notice 2 edittexts have duplicate ids. seemed not problem since use r.id.myedittext
on activity initialization find views test1
or test2
layouts being roots findviewbyid
, , after access these edittexts via view objects directly. find butterknife convenient this, code looks that:
class layoutholder { @injectview(r.id.myedittext) edittext edittext; // ... static layoutholder create(view root) { layoutholder holder = new layoutholder(); butterknife.inject(holder, root); return holder; } }
the problems emerged when tried change screen orientation: both edittexts restored same state due these non-unique ids. ok, thought, have define ids in values
directory , use view.setid
method on edittexts. there issues approach though: in particular, have set these ids manually , programmatically each , every view id within mylayout.xml
. more, if that, not able access these subviews in uniform way anymore since have different ids (in particular, won't able these nice butterknife injections).
i've come other ways of resolving this, more complex , ugly. so, there nice , generic way accomplish needs? wonder wasn't able find on web, because reusing layouts in order avoid code duplication (xml duplication actually, whatever) seems quite natural thing.
patch around butterknife, support 2 ids this
@injectview(r.id.mylayout, r.id.myedittext)
Comments
Post a Comment