data binding - Android dataBinding - How to get resource id of a View in xml -
in android studio data binding works , set fine. have boolean defined this:
<resources> <bool name="showads">false</bool> </resources>
and in layout.xml file referenced boolean (which works fine) want assign id based on boolean. let me show trying accomplish:
i have button in relativelayout tag , depending on boolean reposition button. have this:
<button android:id="@+id/startbutton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textsize="64dip" **************** android:layout_below="@{@bool/showads ? @+id/adone : @+id/title_main}" **************** android:layout_centerhorizontal="true" android:textcolor="#0080ff" android:text="@string/start_btn_title" />
see want to do? want layout button below layout called adone if showads boolean true, otherwise place below layout called title_main. whats syntax have here not compiling. compile error:
expression expected after second @ sign
the above same problem in how dimensions dimens.xml
none of layoutparams
attributes have built-in support. answered in linked article, data binding of layoutparams
thought easy abuse left out of built-in bindingadapters
. not abusing it, should add own.
@bindingadapter("android:layout_below") public static void setlayoutbelow(view view, int oldtargetid, int newtargetid) { relativelayout.layoutparams layoutparams = (relativelayout.layoutparams) view.getlayoutparams(); if (oldtargetid != 0) { // remove previous rule layoutparams.removerule(relativelayout.below); } if (newtargetid != 0) { // add new rule layoutparams.addrule(relativelayout.below, newtargetid); } view.setlayoutparams(layoutparams); }
as aside, @+id/adone in binding syntax not create id. should create id in view you're binding to.
Comments
Post a Comment