I want to set margins based on dimensions i have created in dimens.xml The dimensions it sself works fine, its just data binding cant find it in the 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 it but it says that marginbottom cannot take type float. How can i fix this? I tried casting both dimens to int but then it complains that it cannot be casted to int.
My dimensions xml file looks like this:
<resources> <!-- Default screen margins, per the 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>
dimens. xml is used for defining the dimensions for different widgets to be included in the Android project.
To create another dimens. xml file, right click your res folder and choose New > Value resource file. Then write in dimens and choose Smallest Screen Width. Write in 600 for the width (7” tablet).
The problem here is not with dimensions, but with android:layout_marginBottom
. There is no built-in support for any LayoutParams
attributes. This was done to remove the "foot gun" that many might use to bind variables to LayoutParams
and maybe attempt to use data binding to animate their positions this way.
Data Binding is perfect to be used in your example and you can easily add your own. It would be something like 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, also add the left, top, right, start, and end BindingAdapters
as well.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With