views:

41

answers:

2

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.

A: 

Have you tried "@dimens/titleFont"?

raybritton
Thanks for the response. Yes, I have tried "@dimens/titleFont"(Eclipse changes it to "dimension" when the entry field loses focus). But get this. I thought, maybe, the name of the attribute had to be lower case, so I changed the dimens.xml name of the value to "titlefont," from what I was using "titleFontSize." When I went to change it in the TextView, Eclipse now changes it back to "titleFontSize" after I enter "titlefont." So now I dunno what to think...
Tim
A: 

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"&gt;
    <dimen name="someDimension">5dp</dimen>
    <dimen name="anotherDimension">10dp</dimen>
</resources>
Nailuj
THANK YOU! PROBLEM SOLVED-- program now working. So I use the XML element name, should'a thought of that...
Tim
@Tim: Glad I could help! You should mark this answer as accepted, as it solved your problem. As well as consider an up-vote if you found it to be a good answer :)
Nailuj
I'm happy to mark the question as answered and give you an up-vote (so you can save them up to get a free toaster or whatever) but I don't see where to do these tasks on the page...
Tim
@Tim: At the left of each answer, there is an up- and down-arrow (and a counter) you can click to either up- or down-vote an answer. You as the question asker will also see a "checked" mark, which you can use to mark the correct answer to your question. To read more about question asking, reputation, and how it all works together, I recommend reading the faq: http://stackoverflow.com/faq :)
Nailuj
Thanks again. Yeah, okay, now that I see that Stack Overflow is useful, shucks, I'll read the faq :) I tried to give you a point but I don't have the "reputation" yet...you'll have to content yourself with that warm feeling of being helpful to a stranger...
Tim
@Tim: helping strangers is what Stack Overflow is all about. Enjoy :)
Nailuj