In a drawable XML file, I want to use a dimension resource "background_radius".
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:type="radial"
android:startColor="#FFFFFF"
android:endColor="#00000"
android:gradientRadius="#dimen/background_radius"
/>
</shape>
The dimension resource is defined in Values\Dimens.xml
<?xml version="1.0" encoding="utf-8" ?>
<resources>
<dimen name="background_radius">400dp</dimen>
</resources>
I got the following error when I build it.
No resource found that matches the given name (at 'gradientRadius' with value '#dimen/background_radius'). ...\Resources\drawable\background.xml
But when I design it in VS, I can see the correct background radius. It seems VS just cannot build it.
What's wrong?
Thanks.
+++++++++++++++++++++++++
For some unknown reason, the project did not pick up the new dimens xml file. I re-created the files and rebuild it. Now the dimen can be found.
In addition, the radial gradient does not like the dimen value with dp. so I update the dimen resource to:
<item name="background_radius" format="float" type="dimen">400</item>
Give your dimension a dimension unit (dp, sp, etc.)
http://developer.android.com/guide/topics/resources/more-resources.html#Dimension
A dimension value defined in XML. A dimension is specified with a number followed by a unit of measure. For example: 10px, 2in, 5sp.
So, change it to:
<?xml version="1.0" encoding="utf-8" ?>
<resources>
<dimen name="background_radius">400dp</dimen>
</resources>
for example.
I came across a similar thing; created a Colors.xml file and everything was as it should be but the resource could not be found. After a while I forgot about it and found that it had finally been picked up. Today made a Dimens.xml and the exact same thing happened.
I had to restart Visual Studio in order for it to be picked up.
Related
I want to use a different string resource on small screens but Android always uses the default string. Am I doing it wrong?
/res/values/strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="continue">Continue</string>
</resources>
/res/values-large/strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="continue">Continue to Next Page</string>
</resources>
#string/continue always resolves to "Continue", even on large screens. It does, however use the dimensions in /res/values-large/dimens.xml so I'm sure I'm using the right resource qualifier. I also tried /res/values-w550dp/strings.xml and it didn't work either.
Am I doing something wrong or is this a limitation of Android?
values-large is only available for dimens and styles, but not for string resources.
one could possibly add custom string-arrays and then look them up accordingly.
see Android Resource Types.
Hello community,
I want to iclude different drawable xml-files into my activity_main.xml
However,when I want to includ following code :
<?xml version="1.0" encoding="utf-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<size
android:layout_height="110dp" />
</shape>
</item>
</selector>
the name is button_height.xml into my main-file:
<Button
android:id="#+id/cmda1"
android:layout_width="110dp"
android:layout_height="#drawable/button_height" />
It does not work and Eclipse says only:
You must supply a layout_height attribute.
and
error in an XML file: aborting build.
I searched an answer in the internet but dont found one.
But I think its an easy mistake lots of people are doing.
so, I hope for usefull answers
and sorry for my awful english.
Your whole idea of drawable, selector and dimension is messed up. Can't put height in selector and also selectors (which are drawable) as height.
Do this.
If you want to use xml to get height
Add a file in res folder with name res/values/dimens.xml and add this line in that
<resources>
<integer name="btn_cmda_height" >110dp</integer>
</resources>
and change the button cmda1 height property as
android:layout_height="#dimen/btn_cmda_height"
Make it dynamic for various screen sizes by creating these files which will hold different values. Although this is the old way.
Read more about new way to do this here
res/values/dimens.xml
res/values-small/dimens.xml
res/values-normal/dimens.xml
res/values-xlarge/dimens.xml
If you don't want to use xml to get height
just do this android:layout_height="110dp" in the button cmda1 height property
NOTE:
Your selector code is wrong and not required at all. Selectors are used to define the background state of a view
Resources$NotFoundException: File res/color/dark.xml from drawable resource
I have that error. I tried clean project. I can see "dark" in "R" file. I can use it in project: I mean autocomplete working well, but when I turn on app on emulator there is this error. There is how I use it:
songList.setSelector(R.color.dark);
The correct way to use setSelector() is:
Create a xml in res/drawable
For example, let res/drawable/selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true"
android:drawable="#color/black" />
</selector>
Then declare black in your res\values\strings.xml
<color name="black">#000000</color>
Then set selector as
songList.setSelector( R.drawable.selector);
Note: Answer ideas taken from this post.
EDIT:
Try cleaning your project in Eclipse and re-starting Eclipse.
You're putting your drawable resource in the res/color folder. You should be putting that in the res/drawable folder.
Eclipse is able to render my layout when it includes:
...
<shape>
...
<stroke>
...
android:width="#strings/someWidth"
where someWidth is defined in strings.xml as
<string name="someWidth">5dp</string>
but it crashes on a real device, and only works if I use a direct string, like:
android:width="5dp"
How can I use a resource for the stroke width?
Try to use the dimension resources.
Create a dimens.xml file inside your values folder with the following:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="some_width">5dp</dimen>
</resources>
You can refer to this using #dimen/some_width.
I have a simple layout as follows:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="#+id/widget30"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/translucent_red"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
My colors.xml file is as follows:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="opaque_red">#f00</color>
<color name="translucent_red">#80ff0000</color>
</resources>
When I compile in VS2010 I get an error "System.InvalidOperationException: Command failed...." I have tried to place the colors.xml in res/values and res/color but I still
get the error. If I reference the color directly like:
android:background="#80ff0000"
everything works fine.
Does anyone know if the colors.xml is supported by MonoDroid and if so why I'm getting this error?
Thanks for the help!
Good you found this out. Another issue point to take into account is that you always define the color id's with lowercase letters. I used capital casing and although my c# code finds the resources with case sensitive searching, android expects lowercase id's when referring from drawable or layout xml files (using #color/the_id_here).
Figured it out, the Build Action for colors.xml was set to Content instead of AndroidResource !