2

Maybe a stupid question but in android how can I set aspect ratio in xml (dimens.xml in values folder) to set different aspect ratio depending on devices

I tried :

layout.xml

app:layout_constraintDimensionRatio="@dimen/common_image_aspect_ratio"

values/dimens.xml

<dimen name="common_image_aspect_ratio">3:2</dimen>

but it doesn't seem to work.

Any help would be appreciated, thx :)

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
sokarcreative
  • 536
  • 7
  • 18

2 Answers2

5

Try to write 1.5 instead of 3:2. Generally, you should use decimal format, not fractional. Hope it will help.

Muhammadjon
  • 1,476
  • 2
  • 14
  • 35
0

If you want to define a resource for in the form 3:2 you should use a String:

// values.xml
<string name="common_image_aspect_ratio">3:2</string>

// layout.xml
app:layout_constraintDimensionRatio="@string/common_image_aspect_ratio"

Using a String also applies when setting a ratio programatically:

 (view.layoutParams as ConstraintLayout.LayoutParams).dimensionRatio = "3:2"
patrick.elmquist
  • 2,113
  • 2
  • 21
  • 35