Question

I want to create a dimension that would be equal to 'wrap_content' constant.

So according to developer.android.com Reference I write:

<dimen name="horizontal_border_height">-2</dimen>

But ADT says:

Error: Integer types not allowed (at 'horizontal_border_height' with value '-2')

Asigning 'wrap_content' value generates error too.

What am I doing wrong? Any ideas how to make it work?

Was it helpful?

Solution

To use wrap_content or match_parent you need to create following items in dimens.xml file:

<item name="match_parent" format="integer" type="dimen">-1</item>
<item name="wrap_content" format="integer" type="dimen">-2</item>

Then you can simply use it like this:

<dimen name="layout_height">@dimen/match_parent</dimen>
<dimen name="layout_width">@dimen/wrap_content</dimen>

OTHER TIPS

Use this, it works for me

<integer name="custom_wrap_content">-2</integer>
<dimen name="horizontal_border_height">@integer/custom_wrap_content</dimen>

Please use "-2dp" in dimension instead of "-2".
That is, just add dp after -2.

Check out app resources API guide and you can see supported unites for a dimension value. You can't use dimension to pass a wrap_content as a Views dimension.

You can do like this

<item name="match_parent" format="integer" type="dimen">-1</item>

<item name="wrap_content" format="integer" type="dimen">-2</item>

<dimen name="layout_height">@dimen/wrap_content</dimen>

As far as I'm aware you can't. Here are valid dimension types:

http://developer.android.com/guide/topics/resources/more-resources.html#Dimension

A dimension value defined in XML. A dimension is specified with a number followed by a unit of measure. For example: 10px, 2in, 5sp

Valid units:

dp, sp, pt, px, mm, in

I would just put wrap_content directly in my xml layout or style since wrap_content is wrap_content whatever device/configuration you have.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top