Search code examples
android-studioandroid-layoutdimension

I want to reference one dimension in another one but use an operator on it


As in the title I want one dimension referenced in another one but want to use an operator on it. Something like this

<dimen name="corner_radius">8dp</dimen>
<dimen name="correction_for_corner_radius">-"@dimen/corner_radius"</dimen>

or this

android:layout_marginBottom=-"@dimen/corner_radius"

Solution

  • Essentially, you can't just using XML. The @dimen/corner_radius is actually just an ID, so negative of that is not what you want.

    I'd recommend instead doing it at runtime, e.g. setting margin bottom in your Java / Kotlin to:

    -getResources().getDimension(R.dimen.corner_radius)