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.
Related
I'm trying to create a custom attribute for my control. Here's my attrs.xml:
<?xml version="1.0" encoding="utf-8" ?>
<resources>
<declare-styleable name="ImageView">
<attr name="testcustom" format="reference"/>
</declare-styleable>
</resources>
In my test app at root, I add this namespace xmlns:TestCustom="http://schemas.android.com/apk/res-auto"
Later in my layout file I have an image view
<ImageView
android:src="#drawable/Icon"
TestCustom:testcustom="#drawable/Icon"/>
The first android:src property is fine, however TestCustom does not work.
The error given is "No resource found that matches the given name (at 'testcustom' with value '#drawable/icon)
So... what's going on here? Anyone have any ideas?
You need to specify your xmlns: to your specific package namespace. For example:
xmlns:custom="http://schemas.android.com/apk/res/com.example.customviews"
Represents custom: for the package namespace of com.example.customviews.
You can read more about this here: https://developer.android.com/training/custom-views/create-view.html#customattr
Note: You may want to consider following the convention of camelCase with regards to your custom attributes, views, etc.
So to recap: The solution here was to use lower case name when referencing the resource via a custom attribute. Apparently Xamarin is doing some work in the background that normally allows resources that start with upper case to be OK, but it probably doesn't address this for custom attributes.
If you are using Xamarin, and custom attributes and you can't reference your resource, try it in lower case!!!
Or as Jon recommends, don't use upper-case stuff at all for resources.
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'm trying to have an image which preserves its size in all the resolution except one... what i would like is to use the "wrap_content" attribute for height and override it with a dimen attribute but just for a particular case (like values-xlarge). I did like this:
<ImageView android:id="#id/image_frame"
android:layout_width="match_parent"
android:layout_height="#dimen/overlayImageHeight" />
But of course it crashes if i don't define the overlayImageHeight in all the dimen.xml files.
I tried to put a "wrap_content" string for other dimen.xml files but again it fails since dimen files just accept numeric values inside.
Is there a way to define this behaviour inside an xml file? Other way to do it programmatically?
Any help appreciated!
You should give it a style, define the style in res/values/styles.xml, then make a different version of the style in res/values-xhdpi/styles.xml.
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
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>