data binding - Android databinding - How to get dimensions from dimens.xml -


i want set margins based on dimensions have created in dimens.xml dimensions sself works fine, data binding cant find in case below:

<textview            android:id="@+id/title_main"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_centerhorizontal="true"         android:layout_below="@+id/disablebutton" *************         android:layout_marginbottom="@{@bool/showads ?  @dimen/frontpage_margin_ads: @dimen/frontpage_margin_noads}" *************         android:gravity="center_horizontal"         android:text="@string/app_name"         android:textcolor="@android:color/holo_orange_dark"         android:contentdescription="@string/app_name"         android:textsize="64sp"         android:textstyle="bold" /> 

it did find says marginbottom cannot take type float. how can fix this? tried casting both dimens int complains cannot casted int.

my dimensions xml file looks this:

    <resources>      <!-- default screen margins, per android design guidelines. -->     <dimen name="activity_horizontal_margin">16dp</dimen>     <dimen name="activity_vertical_margin">16dp</dimen>     <dimen name="bigtext">44sp</dimen>     <dimen name="littletext">44sp</dimen>     <dimen name="mediumtext">40sp</dimen>         <dimen name="smalltext">24sp</dimen>     <dimen name="fab_margin">16dp</dimen>     <dimen name="frontpage_margin_noads">0dp</dimen>     <dimen name="frontpage_margin_ads">13dp</dimen>   </resources> 

the problem here not dimensions, android:layout_marginbottom. there no built-in support layoutparams attributes. done remove "foot gun" many might use bind variables layoutparams , maybe attempt use data binding animate positions way.

data binding perfect used in example , can add own. this.

@bindingadapter("android:layout_marginbottom") public static void setbottommargin(view view, float bottommargin) {     marginlayoutparams layoutparams = (marginlayoutparams) view.getlayoutparams();     layoutparams.setmargins(layoutparams.leftmargin, layoutparams.topmargin,         layoutparams.rightmargin, math.round(bottommargin));     view.setlayoutparams(layoutparams); } 

you would, of course, add left, top, right, start, , end bindingadapters well.


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -