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>
Related
I have some problem with setting params to my view in xml file with values from dimen files.
For example when i'm adding layout_height param to my EditText in laytout xml file:
<EditText
....
android:layout_height="#dimen/et_height"
....
/>
File dimens:
<resources>
.........
<dimen name="example">16dp</dimen>
<dimen name="et_height">20dp</dimen>
.........
</resources>
It works fine. But sometimes when i open this file again AndroidStudio replace #dimen/et_height with value 20dp in layout xml file. And i must change it again to #dimen/et_height.
How can i fix this ?
This is the normal case:
But in my case AndroidStudio replaces with value:
Having the same issue. Thought it is the ConstraintLayout but the issue also appears in every other layout though..
related post from me :
Android ConstraintLayout #dimens replaced with hardcoded values
Also some other user said it just shows the dp value, i am completely in your situation i know the grey text, but it does really replace the value..
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.
I have an XML file with 5 textviews, all with the same textSize. Currently, I define the size like this:
android:textSize="30dp"
However, I want a quick way to apply something like:
android:textSize="text_size"
Where textSize would be a variable I define somewhere, and can use wherever I want in my XML file. I tried defining these variables in the strings.xml file in the Android project, and calling them like this:
android:textSize="#string/text_size"
Where it would be defined like this in strings.xml
<string name="text_size">30dp</string>
It worked fine in Ecplise itself, and showed just fine in the Graphic Layout. But when I tried running the app on my phone, it crashed.
So, is there a way to define variables in my XML layout files? Thanks.
Your idea of putting it in strings.xml is close, but those values need to be set in a dimensions XML file. The documentation is here.
Using your example, you might make a file called res/values/my_dimens.xml. Then put in there
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="my_text_size">30dp</dimen>
</resources>
Then to use it in your layout, just set
android:textSize="#dimen/my_text_size"
I am teaching myself Android using Eclipse, the Android plug-in, and Sams "Teach Yourself Android Development" book. I have this weird little problem. I've been able to create xml files in the res/values directory that hold strings and color values (colors.xml and strings.xml). I have been able to reference these values in the properties of my Android screens (the xml in res/layout), for example setting the "Text" and "Text color" properties with references like "#string/topTitle" and "#color/titleColor," where topTitle and titleColor are defined in the xml files.
BUT: when I create a file called "dimens.xml" and have font sizes in it, Eclipse correctly puts this file in res/values, but when I try to reference these values, e.g. "#dimension/titleFont" I get an error "No resource found that matches the given name." I've tried lots of different names, I've tried "#dimens" instead of the type, still nothing. If I go into the layout xml file and set it explicitly to a font size, e.g. 22pt, it works.
So Eclipse recognized my "dimens.xml" file when I made it well enough to put it in res/values, and lets me edit it, and shows it full of (dimension) values. It just doesn't recognize my referring to it in other xml files.
The book I'm using doesn't actually show a dimension example so I must be doing something wrong. I checked the Android docs but couldn't see any problem.
Any help appreciated. Thanks.
The correct way to refer to a dimension variable (stored in your dimens.xml (don't think the name here really matters though, it's what's inside that does)) from another xml file is like this:
"#dimen/nameOfVariable"
Notice that it is neither dimension, dimensions or dimens, but dimen!
If you look inside your xml file where you have your values, this will make sense as dimen is the name of the xml elements storing dimension values:
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<dimen name="someDimension">5dp</dimen>
<dimen name="anotherDimension">10dp</dimen>
</resources>
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.