Android - ConstraintLayout percentage using dimens
Asked Answered
I

4

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?

Intinction answered 11/4, 2017 at 11:49 Comment(0)
I
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>

Intinction answered 11/4, 2017 at 11:49 Comment(8)
If this works, then that's either an IDE bug or a build tools bug, as both approaches result in a dimension resource.Clinch
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.Intinction
Ah, I see. Try using a float or integer resource, then, rather than dimen.Clinch
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.Intinction
Hmmm... I could have sworn that there were float resources, though I am not seeing them in the docs right now...Clinch
Are you thinking of fraction? E.g., <fraction name="guideline_perc">0.67</fraction>. Used like app:layout_constraintGuide_percent="@fraction/guideline_perc"Certes
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: <item name="guideline_perc" type="dimen" format="float">0.67</item> Certes
hwo do I reference this?Algarroba
H
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"
Haemato answered 20/11, 2018 at 14:12 Comment(1)
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.Vidal
M
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"

Marvelmarvella answered 2/6, 2021 at 13:55 Comment(0)
M
0

Generated by the system itself

<item name="porcent_line_top_navebar" type="dimen" format="float">0.88</item>
Malmo answered 21/8, 2021 at 14:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.