3

I know this is a simple question, so hopefully it won't be a bother to anyone.

How can I have a constant float value in my project that I can access from a View's XML and .java file?

I've tried setting a float value in my View's resource file as such:

<attr name="cornerRadius" format="float">15.0dp</attr>

I believe thats how it should be done, but I just did it using intuition. I'm really struggling on what to google on this subject.

Now, for accessing it in Java, I've tried this:

float myFloat = getContext().obtainStyledAttributes(attrs, R.styleable.MyView).getFloat(R.styleable.MyView_cornerRadius, 0.0f);

This is throwing me off, since its not getting the right value, but rather the value that I pass in the function. Why am I passing a float value to this function? There must be a different option.

Now, where the biggest problem is for me is accessing it in the XML.. I've tried this:

<corners android:radius="@attrs_my_view/cornerRadius"/>

I also tried this:

<corners android:radius="@float/cornerRadius"/>

But to no avail.

Maybe all of this is completely wrong, but I don't know how to search for this in google, because I don't know what its called. Its like having to look up a word in a dictionary based on its meaning. Its not a good time.

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
RileyE
  • 10,874
  • 13
  • 63
  • 106
  • 1
    See http://stackoverflow.com/questions/3441396/defining-custom-attrs for information about defining custom attributes. I hope this helps. – Code-Apprentice Jan 10 '13 at 21:03
  • @Code-Guru Yes! That is quite helpful, although, I'm still thrown off by the getFloat(int, float) method in TypedArray. – RileyE Jan 10 '13 at 21:40
  • For what it's worth, I found that link by googling "attr android". Google searches don't have to be complicated ;-) Also feel free to edit your question or post a new one if you need further clarification. – Code-Apprentice Jan 10 '13 at 21:52

1 Answers1

1

You can create an XML file to contain your constants. This sounds like a dimension, which are standardly stored in the dimens.xml file. Place this in your res/values folder.

So, you might have a file (named dimens.xml), with the following in it:

<resources>
    <dimen name="cornerRadius">15dp</dimen>

</resources>

Then you can reference it using the normal R.class (ie. R.dimen.cornerRadius) - which you can reference from XML or Java.

Booger
  • 18,579
  • 7
  • 55
  • 72
  • Dimensions are a floating-point value and a unit of measure, not just a floating-point value. – CommonsWare Jan 10 '13 at 17:22
  • I have generally used DP values here (ie. 180dp). Not sure how this would work with floats (can you clear that up for me - and I will correct the answer) - or can you edit it? – Booger Jan 10 '13 at 17:23
  • Again, dimensions are a floating point value and a unit of measure (e.g., `15.3dp`). I know of no way to represent a plain floating-point value as a dimension resource. I also do not see a `float` resource type documented, though it is possible that one exists and is not documented. – CommonsWare Jan 10 '13 at 17:27
  • @CommonsWare Well, so long as I use px, dp, sp, pt, etc. I should be fine, right? The only reason I need the constant, in this case, is for UI, so I am comfortable using the dp, px and sp formats. But, I am so lost on all of this, I said float in my question. I now know better! – RileyE Jan 10 '13 at 17:31
  • @Booger I knew it was simple! Thanks! However, the only use case for my question would pertain to pixel/size related situations, so you may want to change the `f` to a different type (probably dp). – RileyE Jan 10 '13 at 17:34
  • @RileyE: I do not know where you are using this. The only `` element I know of off the top of my head is for `ShapeDrawable`, and that takes a dimension, not a plain float. – CommonsWare Jan 10 '13 at 17:35
  • @CommonsWare What `dimension` are you talking about? How would I declare and obtain that programmatically? – RileyE Jan 10 '13 at 17:44
  • @RileyE: Using a dimension resource, as per the answer. Please read the documentation for `ShapeDrawable`: http://developer.android.com/guide/topics/resources/drawable-resource.html#Shape and for dimension resources: http://developer.android.com/guide/topics/resources/more-resources.html#Dimension – CommonsWare Jan 10 '13 at 17:52
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/22539/discussion-between-rileye-and-commonsware) – RileyE Jan 10 '13 at 17:53
  • Changed my original answer to have dp (which is how I have always used this anyway). – Booger Jan 10 '13 at 18:44