0

One of the requirements of my code is that an alpha value is used. I want to program my application so that at some point in the future, I can easily change this value.

My understanding is that resources are built specifically for this purpose. Alpha needs to be between 0 and 1.

I wondered if there was a more elegant solution than setting the value to an integer between 1 and 100 in the integer resource folder, then dividing by 100 in the code.

Current code:

<?xml version="1.0" encoding="utf-8"?>
<resources>    
<!-- choose a value for alpha, it will be divided by 100. 
valid values lie between 0 and 100.-->
<item name = "exponential_filter_alpha" format="">9</item>    
</resources>

I saw a similar question here: https://stackoverflow.com/a/8780360/1014849 But that required floats, doubles are not available via that method.

Community
  • 1
  • 1
broomi
  • 27
  • 7

3 Answers3

1

There is an integer resource. See here: Resource Types - More Types. Then you can call getResources().getInteger(R.integer.exponential_filter_alpha) in your Activity to get the integer value, then divide by 100 as you requested.

Chris Feist
  • 1,678
  • 15
  • 17
  • Thank you, this is what I was currently using but wondered if there was a more elegant method. – broomi May 22 '14 at 16:54
  • 2
    I don't understand how you get more elegant that that. Its pretty straightforward and you have all your values in one spot as opposed to hard-coding them in different areas. You don't need to have all your values in different files either. You can have ``'s and ``'s all in a common constants.xml or config.xml file in your `res/values` folder. If your curious, you can always see how Google organized their [values folder](http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.3_r1/frameworks/base/core/res/res/values/). – Chris Feist May 22 '14 at 18:09
  • Yes we're in agreement with regards to the resource folder route. What would be perfect would be if I could store doubles in the resource folder, which unfortunately I cannot . I was looking for a solution to this problem. Storing floats in the resource folder turns out to be the best (seemingly). Thank you very much for your input Chris. – broomi May 23 '14 at 10:17
1

Based on your comment, I understand your questions as a performance question.

Saving and using an int in resources instead of a float should not be noticeable in the performance at all. By that note I think saving an int and then dividing by 100 all the time would be silly, when you could just as well save a float. Also noting that when you divide your int it will get implicitly converted to a double anyway.

You say that you use the value for an alpha value, and you might only need the precision 1 to 100, using a float for that kind of precision is very reasonable, and also reasonable for an alpha value.

You might want to read Float or Double?

Community
  • 1
  • 1
  • You are correct, it is a performance question. Thank you. The link was helpful and I appreciate your input. I was coming at it from a best practice pov. – broomi May 23 '14 at 10:15
0

Just use:

<item name="prim_alpha" format="float" type="dimen">0.87</item>
Peter
  • 10,910
  • 3
  • 35
  • 68