I'm not able to find a way to insert the action bar size attribute in my own xml file. Since I'm using Google maps in my activity, I can't use the built-in xml attribute at all in my layout, but I have to call setPadding() on the map using the action bar size. Now, I found the way to retrieve the actionBarSize at runtime, but I wonder if I could skip this code completly, inserting the android attribute in my onw xml, for example:
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<dimen name="myownsize">#android:attr/actionBarSize</dimen>
</resources>
But in this way it doesn't work. Is there a way to do it?
Define a attr in your own project. Create res/values/attrs.xml and add this:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<attr name="myownsize" format="dimension"></attr>
</resources>
In your res/values/styles.xml, under your parent theme definition, initialize this attribute:
<style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="myownsize">?android:attr/actionBarSize</item>
</style>
Once this is done, you can use ?attr/myownsize as a dimension:
android:padding="?attr/myownsize"
Sorry if I too, misunderstood the question.
Related
I'm trying to set a ListView background color based on the current theme attribute, but it crash every time the ListView is shown.It seems I'm doing something wrong but I can't see what...
Here's what I'm doing:
First, create the background color:
<resources>
<color name="userlist_background_light">#fff0f0f0</color>
<color name="userlist_background_dark">#ff040404</color>
</resources>
Second, create attributes for my custom themes:
<resources>
<attr name="userlist_background" format="reference|color" />
</resources>
Third, setting this attribute in my themes:
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="Light" parent="Theme.Sherlock.Light">
<item name="userlist_background">#color/userlist_background_light</item>
</style>
<style name="Dark" parent="Theme.Sherlock">
<item name="userlist_background">#color/userlist_background_dark</item>
</style>
</resources>
And finally, using this attribute in the ListView xml:
<ListView
android:id="#+id/user_bar"
android:layout_width="0dip"
android:layout_height="0dip"
android:background="?attr/userlist_background"
android:cacheColorHint="?userlist_background"
android:visibility="gone" />
Even the Eclipse layout view crash. Of course, it works fine if I use a "#color/" directly in the background attribute. It even work if I use say, "?android:attr/colorBackground".
The message error is:
android.view.InflateException: Binary XML file line #8: Error
inflating class android.view.ListView Caused by:
android.content.res.Resources$NotFoundException: Resource is not a
Drawable (color or path): TypedValue{t=0x2/d=0x7f010068 a=-1}
I'm pretty sure I'm doing something wrong, as it works with android attributes, but I haven't be able to find what during my Google searches.
I hope you'll be able to help me!
Many thanks,
Sébastien.
Ok, I fixed it, and it was due to a mistake!
I have two themes.xml files, one for Honeycomb+, and one for Gingerbread-. I've only added the new attributes to the themes.xml targeting Gingerbread-, and was testing on ICS.
Maybe it'll help others who'll make the same mistake!
I'm a using a view flow component into my project, which allows the dev to override some attributes, like that :
<org.taptwo.android.widget.TitleFlowIndicator
...
app:footerTriangleHeight="0dip"
... />
I am reusing this component into several layouts, and I would like to put the properties into a style.
But when I'm doing this, the parser says Error: No resource found that matches the given name: attr 'app:footerTriangleHeight', even if I add the namespace in the styles file.
Is there a way to do that in android ?
Thanks.
Android Application
If you are not using Android Libraries, then here is what you can do:
Define custom styleable attribute (I guess you've already done that).
Do not use your namespace prefix in style items (namespace defaults to current app's namespace).
Example:
In attrs.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<attr name="testAttr" format="string"/>
</resources>
In styles.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="TestStyle" >
<item name="testAttr">asdf</item>
</style>
</resources>
Android Library
If custom attribute comes from Android Library, you can still use described approach. It theoretically should work, because Android Library's namespace is the same as application's (from aapt tool perspective during the build). But I haven't test this myself.
If you're specifying namespace, it will show error. As far as I know, styles do not support xml namespaces. So this will fail:
In styles.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:app="http://schemas.android.com/apk/res/app.package.name">
<style name="TestStyle" >
<item name="app:testAttr">asdf</item>
</style>
</resources>
Parser automatically defaults to current app's AndroidManifest namespace, though you can not specify this namespace explicitly.
You can define the style in terms of your namespace, i've used it myself
i.e
<style name="whatever">
<item name="YourPackage.YourSubPackage:parametername">#drawable_or_anything/bla</item>
</style>
Howdy.
In my themes.xml definition, I have the following:
<style name="mythemename">
<item name="d_myvar">100dip</item>
</style>
I would like to be able to reference this in res/values/dimens.xml like so:
<dimen name="myvar">?d_myvar</dimen>
Alas, this doesn't work. When I try to use the #dimen/myvar as the height of a LinearLayout, the app crashes with the error "You must supply a layout height attribute."
I have also tried
<dimen name="myvar" value="?d_myvar" />
But that won't compile.
How can I define #dimen/myvar in my xml so that it loads the ?d_myvar variable defined in the theme?
Thanks!
I saw your help request on the Italian Startup Scene.
Unfortunately, according to the syntax of the dimen tag:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen
name="dimension_name"
>dimension</dimen>
</resources>
you simply can't do it. In fact, you can reference theme attributes when the syntax specifies:
?[package:][type:]name
Solutions:
Gix's answer would be the standard way of defining and reusing dimensions.
Maybe you can reorganize your code to reuse d_myvar through inheritance?
As a last and desperate resort, I would go for a shell script that automates the process of variable substitution using xml command line tools. I have never personally used them, but see for example xmllint, xmlstarlet or this article.
Put the 100dip part in your dimens.xml like so:
<dimen name="myvar">100dp</dimen>
then in your theme you can reference it as #dimen/myvar if you need, or you can reference it in code using R.dimen.myvar
In other words, you don't set the dimension in theme and then reference it in dimens.xml, but you go the other way around. You set the dimension in dimens.xml and then reference that in your theme/style xml.
In styles.xml
<resources>
<attr format="dimension" name="exampleDimension"/>
<style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
<item name="exampleDimension">100dp</item>
...
</style>
</resources>
Then in your other xml to use the new attribute, you would use it like this
android:padding="?attr/exampleDimension"
I have a few custom views in my Android project and I've added the relevant details to the attrs.xml file. And now I can implement my objects through XML. This works fine.
How do I style these elements? When I try to use my custom attributes in the styles.xml is get an error "No resource found that matches the given name:"
For using the custom views in normal xml developement I use xmlns:app="http://schemas.android.com/apk/res/bla.bla.bla". What is the correct for use in styles?
This is what my style looks like currently
<style name="Journey_DaySelect_Sunday">
<item name="app:onImage">#drawable/day_sunday_selected</item>
<item name="app:offImage">#drawable/day_sunday</item>
</style>
After more intensive searching on Google I gave up finding it answered elsewhere, and by chance tried using the absolute namespace of my generated R file it worked. May this solve all your problems.
USE THE NAMESPACE CONTAINING YOUR R FILE
<style name="Journey_DaySelect_Sunday" parent="Journey_DaySelect">
<item name="AppZappy.NIRailAndBus:onImage">#drawable/day_sunday_selected</item>
<item name="AppZappy.NIRailAndBus:offImage">#drawable/day_sunday</item>
</style>
For clarification, the item's name attribute should be the same as what is in the attrs.xml's declare-styleable name attribute + ":" + the attribute name.
For example:
attrs.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="com.chuck.norris">
<attr name="actionBarTextColor" format="color"/>
</declare-styleable>
</resources>
style.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="myNewStyle">
<item name="android:textColor">#FFFF0000</item>
<item name="com.chuck.norris:actionBarTextColor">#ffff0000</item>
</style>
</resources>
You can then apply this style to all activities by using a theme in your manifest.xml file. Anywhere that a custom view exists that wants to use the "actionBarTextColor" attribute, you can then use the Java code:
TypedArray typedArray = context.obtainStyledAttributes(attrSet, R.styleable.com_chuck_norris);
COLOR_ACTION_BAR_TEXT = typedArray.getColor(R.styleable.com_chuck_norris_actionBarTextColor, 0xff0000ff);
typedArray.recycle();
I'm not sure why you cannot just define your schema in your style.xml file as was asked above, but it seems to be a limitation of style.xml.
try this solution
<style name="Journey_DaySelect_Sunday">
<item name="onImage">#drawable/day_sunday_selected</item>
<item name="offImage">#drawable/day_sunday</item>
</style>
reference(Chinese)
if you guys think it useful,I will translate it.
I have a set of custom Android layout parameters defined in attrs.xml. Now I would like to use some tags in my styles.xml file.
At the moment I get this error:
error: Error: No resource found that matches the given name: attr 'custom:tag'
I have tried declaring custom XML namespace as follows:
<resources
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res/com.my.project"
>
hoping, that the same logic used in every layout declaration can be applied here, but with no success.
The XML namespace mechanism is used to namespace tags and attributes. When you define a style like this:
<?xml version="1.0" encoding="utf-8"?>
<resources
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res/com.my.project">
<style name="my_style"> <item name="custom:tag">some_value</item> </style>
</resources>
you are trying to apply XML namespacing to an attribute value, which won't work. In this case, you should specify the package name directly, like this:
<style name="my_style"> <item name="com.my.project:tag">some_value</item> </style>
Now Android will be able to resolve where the attribute is defined.
The accepted solution did not work for me, but it shed some light upon the situation.
The custom attributes are resolved and can be referenced in a global project's package name, like "com.ltst.project". Even if you have multiple modules (with the same base package name) the resources would be resolved in a project's package name.
So for me it was enough to just omit any prefixes for custom attributes in a style.
Custom attribute:
<declare-styleable name="SampleView">
<attr name="sample_color" format="reference" />
</declare-styleable>
Style:
<style name="SampleStyle">
<item name="sample_color">#color/sample_color</item>
</style>
You can use the link
xmlns: app = "http://schemas.android.com/apk/res-auto
and define the prefix for each tag as app