18

There is a question How to make ConstraintLayout work with percentage values? and its answers show how to use the percentages:

<android.support.constraint.Guideline
    android:id="@+id/guideline"
    android:layout_width="1dp"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    app:layout_constraintGuide_percent="0.5"/>

But if you don't want to hardcode the percentage but use a dimen resource it does not work.

<!-- inside the layout -->

<android.support.constraint.Guideline
    android:id="@+id/guideline"
    android:layout_width="1dp"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    app:layout_constraintGuide_percent="@dimen/guideline_perc"/>

<!-- inside dimens.xml -->

<dimen name="guideline_perc>0.5</dimen>

You get the following error:

Float types not allowed (at 'guideline_perc' with value 0.5).

If you replace the value with 1, a similar error is returned:

Integer types not allowed (at 'guideline_perc' with value 1).

How do you set a percentage without hardcoding the value into the layout?

Community
  • 1
  • 1
neits
  • 1,963
  • 1
  • 18
  • 32

4 Answers4

15

Instead of using a dimen resource, use an item resource of type dimen:

<item name="guideline_perc" type="dimen">0.5</item>

If using integers, an integer resource would work best:

<integer name="guideline_perc">1</integer>

neits
  • 1,963
  • 1
  • 18
  • 32
  • 1
    If this works, then that's either an IDE bug or a build tools bug, as both approaches result in a dimension resource. – CommonsWare Apr 11 '17 at 11:52
  • 1
    Defining a resource as a dimen requires a unit of measure. Defining it as an item with the type `dimen` does not. Additionally a format `float` could be added but it is not necessary. – neits Apr 11 '17 at 13:09
  • Ah, I see. Try using a `float` or `integer` resource, then, rather than `dimen`. – CommonsWare Apr 11 '17 at 13:12
  • 1
    Using `integer` would be a better solution, that's true, but in this example I was using a float and there isn't a `float` resource afaik. – neits Apr 11 '17 at 15:39
  • Hmmm... I could have sworn that there were `float` resources, though I am not seeing them in the docs right now... – CommonsWare Apr 11 '17 at 15:55
  • Are you thinking of `fraction`? E.g., `0.67`. Used like `app:layout_constraintGuide_percent="@fraction/guideline_perc"` – AutonomousApps Dec 27 '17 at 23:33
  • 1
    Nvm, that doesn't work. AS doesn't complain, and the layout even looks right in the preview, but it won't compile. Need to use this: `0.67 ` – AutonomousApps Dec 27 '17 at 23:42
  • 1
    hwo do I reference this? – DaniloDeQueiroz Aug 31 '18 at 19:45
13

To set a percentage use the fraction syntax

<fraction name="guideline_perc">0.5</fraction>

and then

app:layout_constraintGuide_percent="@fraction/guideline_perc"
Catalin Morosan
  • 7,897
  • 11
  • 53
  • 73
  • 3
    Small point of note: Confusingly, the XML editor does NOT offer _typeAhead_. Red light goes off only when you correctly spell the resource name in full. – Bad Loser Dec 08 '19 at 23:25
1

Now in 2021 dimen float works fine. Just autocompletion in Android studio in that case doesn't. Don't know why.

app:layout_constraintGuide_percent="@dimen/guideline_perc"

Vít Kapitola
  • 499
  • 1
  • 5
  • 9
0

Generated by the system itself

<item name="porcent_line_top_navebar" type="dimen" format="float">0.88</item>
Victor Sam VS
  • 139
  • 3
  • 4