Is it possible to use the reference of a dimension resource in another dimension? What i mean is something like this:
File dimen.xml:
<dimen name="test1">18sp</dimen>
<dimen name="test2">#dimen/test1</dimen>
It works the way i posted
<dimen name="test1">18sp</dimen>
<dimen name="test2">#dimen/test1</dimen>
I would suggest to consider one more thing when using this.
I get what you said in your comment, but either there is a link between the two values or there is not and you should distinguish between these two possibilities.
If there is no link between test1 and test2 and they are independent and the fact that they hold the same value is mere coincidence, then you shouldnt suggest a link between the two. Instead just set the same value twice.
Why do you want create a second dimension with the same value as first ?
Don't create second dimension, but use first !
Related
Can someone let me know, what if there is some dimen by same name in app and a library.
dimen.xml inside app
<dimen name="activity_horizontal_margin">10dp</dimen>
dimen.xml inside library
<dimen name="activity_horizontal_margin">12dp</dimen>
Which one will be used at runtime?
I can try it, but I will not be clear for the reason to being used as final value.
You can try to redefine size in your dimensions.xml:
<resources xmlns:tools="http://schemas.android.com/tools">
<dimen name="activity_horizontal_margin" tools:override="true">12dp</dimen>
</resources>
Use with care as this is a hack, not an actual solution. The solution might not work if the resource's name is changed in the future Library release.
When you redefine(the same name) a dimension resource is like overriding the lib resource. Just like you override a class method.
I think the answer would be:
It depends on what layout is being populated, if it's an activity layout from an app module then app's dimen will be used and vice versa..
This is a great question I don't know why it got downvoted. While the selected answer is true there is slightly more nuance to it, Android chooses the "most specific" resource which takes precedence over app resource values.
e.g. consider the boolean <bool name="isTablet">true</bool> in a library, setup like so:
values - false
values-sw600dp - true
And in an app like so:
values - false
values-sw600dp-land - true
What is the value of the boolean on a 1280x800 portrait tablet when called from the app?
The answer is true, even though the app specified it to be true in landscape only!
I have dozens of ImageViews in my xml file and defined the length in strings.xml such as string name="cardWidth">100dp</string> and then had content_main.xml use that string in the width. Like android:layout_width="#string/cardWidth" that way I could just change the value once and see how all the images displayed.
No problems with the compile but got a run time error
Binary XML file line #XX:
You must supply a layout_width attribute.
I researched and I saw in S.O. that this will occur when you use a string in the value. I'll probably just hard code the value once I'm happy with it but is there a way around this?
Thanks!
Sorry for the formatting, it's my first question here!
Your dimension values need to be defined as (possibly in a file res/values/dimens.xml):
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="cardWidth">100dp</dimen>
</resources>
Then, you can access them using:
android:layout_width="#dimen/cardWidth"
use these width with dimensions in values folder not with string.xml like below :
<dimen name="cardWidth">16dp</dimen>
I don't want to specify hard coded test size values in my layout.xml, hence i am using the following specification :
android:layout_height="#integer/intervalViewHt"
and
#integer/intervalViewHt value is as follows:
<integer name="medium">15</integer>
Now, the while inflating android is creating a problem saying that it cannot inflate the view. I want to actually specify value in dp so the actual value should be like
android:layout_height="15dp"
Can anyone help me here ?
What you're looking for are "dimensions" rather than plain integers.
Declaration:
<dimen name="intervalViewHt">15dp</dimen>
Usage:
android:layout_height="#dimen/intervalViewHt"
Have a look at the given link for more examples in the Android docs.
I use something like this for text size across different devices. It's java based, not xml.
tvOutput.setTextSize(TypedValue.COMPLEX_UNIT_DP, getResource().getInteger(R.integer.medium));
I haven't tested the code so you might have to play with it. I'm sure you could do something like.
button1.setHeight(TypedValue.COMPLEX_UNIT_DP, getResource().getInteger(R.integer.medium));
Though again I haven't tried it and you may need something different then TypedValue
What if you give it a String rather than an int. i.e in strings resource you have a string "15dp" that you reference
Similar to how you can have two folders res/values and res/values-xlarge, and in each you can have a dimens.xml file with different dimensions...
I want to be able to store some integers (not sizes in dp or anything like that, just plain integers) like that. Is this possible to do in .xml? Or if not, how could I do that programmatically?
If you must know why: I am displaying a list where the user can choose to show the number of results at a time. For smaller screens, I want the default list length to be smaller than the default list length for larger screens.
Yes, there's a mechanism exactly like that. Just create an XML file in res/values and related folders and instead of something like:
<resources>
<dimen ...>
</resource>
You can provide an integer resource:
<resources>
<integer name="i">234</integer>
</resource>
However, in your specific scenario I'd also consider if simply changing the related layout file makes more sense.
Sure, you can do that. Just like storing a string Ressource you can store Integers.
Taken from the documentation:
XML file saved at res/values/integers.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<integer name="big_screen_width">12000</integer>
<integer name="small_screen_width">320</integer>
</resources>
And the code would look like that
Resources res = getResources();
int bigScreen = res.getInteger(R.integer.big_screen_width);
You can store things programmatically by using SharedPreferences.
Looking in the android sdk folders, I've found a file called values/config.xml. This seems to be somewhere that you can define values for later use in layouts and animations.
Given the config.xml:
<resources>
<string name="config_somePadding">50dip</string>
</resources>
How would I reference this to use as the layout_height in a layout xml file?
#string/config_somePadding is actually the only one I've found that doesn't throw an error in Eclipse (even though there isn't a config_somePadding in values/strings.xml), but it appears to just put an empty string.
In the sdk, they use an integer for animation duration. They reference it like this: android:duration="#android:integer/config_longAnimTime". Is there a way to use values that aren't integers in the layout_height attribute?
How would I reference this to use as
the layout_height in a layout xml
file?
You wouldn't. Use a dimension resource instead.
Is there a way to use values that
aren't integers in the layout_height
attribute?
You have to use a dimension resource for dimensions.