We want to create an Android Class Library to reuse some code, mainly custom views.
I have successfully created and referenced the views in Xamarin.Android projects.
The only issue I've got is I cannot use the declare-styleable. The view is looking fine, but can't use custom attributes in the XML layout.
<resources>
<declare-styleable name="MyCustomView">
<attr name="srcLittle" format="reference" />
</declare-styleable>
</resources>
And this is how I use it:
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/res-auto"
[...] >
[...]
<Core.MyCustomView
android:id="#+id/item_proposal_validation_trips_icon"
android:layout_gravity="center"
android:layout_height="48dp"
android:layout_width="48dp"
app:srcLittle="#drawable/ic_plane" /> <-- ERROR
[...]
</android.support.v7.widget.CardView>
Error in XML:
The "http://schemas.android.com/apk/res/res-auto:srcLittle" attribute is not declared
Error compiling:
1: error: No resource identifier found for attribute 'srcLittle' in package 'res-auto'
Thanks.
The namespace you've defined for the app prefix in your layout is incorrect.
xmlns:app="http://schemas.android.com/apk/res/res-auto"
The correct namespace for your app-defined attributes is http://schemas.android.com/apk/res-auto. You've got an extra res/ in there. It should be:
xmlns:app="http://schemas.android.com/apk/res-auto"
Related
What is the difference and more importantly the necessity of having different prefixes in Andriod view XML?
For example,
<android.support.v7.widget.Toolbar
android:id="#+id/actionToolBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:contentInsetEnd="20dp"
app:contentInsetEnd="20dp"
android:elevation="3dp"
/>
Has contentInsetEnd for both android and app.
android is usually used for attribute coming from Android SDK itself.
app is often used if you are using the support library.
You may also see other namespaces if you are using custom views (of your own or form a library).
Here is some extra information: http://developer.android.com/training/custom-views/create-view.html#customattr
app namespace is used for custom defined attributes, which are usually defined in /values/attrs.xml Here is a sample of such file
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="SimpleTabIndicator">
<attr name="numberOfTabs" format="integer"/>
<attr name="indicatorColor" format="color"/>
</declare-styleable>
</resources>
And a sample usage would be
<com.someapp.demo.SimpleTabIndicator
android:id="#+id/tabIndicator"
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="#26292E"
app:indicatorColor="#FFFDE992"
app:numberOfTabs="5"/>
Android namespace you use for Android's widgets and UI controls.
app is just a namespace for any custom parameters for a custom View.
This can be anything but if you see the root element there's probably a line xmlns:app="http://schemas.android.com/apk/res-auto" that assigns the namespace .
I've got a question on custom view XML-declarations.
I created my own View with the custom attributes as normal. Now I want to add more complex attributes like this: (This is non-working code)
<com.xxx.yyy.CustomTextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/customTextView1"
android:layout_marginBottom="22dp"
android:layout_toRightOf="#+id/buttonBlack"
android:text="TextView" >
<Animation
animation:property1="123"
animation:property2="456" />
<Animation
animation:property1="789"
animation:property2="012" >
</Animation>
</com.xxx.yyy.CustomTextView>
I didnt find a way to do this on my own but maybe someone has got an idea.
Thanks!
Edit:
I just solved the problem more or less nicely.
I created a new .xml file in my /res/xml folder called animations.xml
<animations>
<animation
name="Animation name 1"
float1="1.1"
float2="1.2"
integer1="11"
integer2="12" />
<animation
name="Animation name 2"
float1="2.1"
float2="2.2"
integer1="21"
integer2="22" />
</animations>
My custom view in attrs.xml contains an attribute which is used to reference the animations.xml file from above:
<declare-styleable name="MyTextView">
<attr name="animations" format="reference" />
</declare-styleable>
Now i parse the referenced .xml file in the constructor of the MyTextView as described here: http://thedevelopersinfo.com/2009/12/14/using-xml-file-resources-in-android/
Maybe this helps somebody at some time.
Unless CustomTextView extends ViewGroup (or has it in it's class hierarchy) and Animation is a custom view you've defined, this will not work. Only Views and ViewGroups are allowed in layout XML files (and some special tags defined by Android like include and merge), and only ViewGroup elements can have child elements.
You can add custom XML attributes to your custom view like this:
<resources>
<declare-styleable name="YourCustomClass">
<attr name="someCustomAnimationAttribute" format="reference" />
</declare-styleable>
<resources>
How you obtain it is described here:
http://developer.android.com/training/custom-views/create-view.html#applyattr
In your layout you have to declare a namespace:
xmlns:app="http://schemas.android.com/apk/res-auto"
and then use it like this:
<com.xxx.yyy.CustomTextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/customTextView1"
android:layout_marginBottom="22dp"
android:layout_toRightOf="#+id/buttonBlack"
android:text="TextView"
app:someCustomAnimationAttribute="#drawable/someAnimation">
That way you can supply those animations via XML instead of adding them as as child elements which is not possible.
I am trying to use custom attributes in some Android layouts, but I am getting an error (from Eclipse) when I try to use a namespace prefix other than android: in a child element. Note that it works ok when I use the custom: namespace prefix in the root/parent element in the file, just not the child element.
For example, here's a simple layout with the custom namespace specified:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
custom:my_tag1="whatever"> <!-- compiles fine -->
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitCenter"
custom:my_tag2="true"/> <!-- generates an error -->
</LinearLayout>
The error that Eclipse gives (only on the second attempt to use the custom: prefix) is:
Unexpected namespace prefix "custom" found for tag ImageView.
If I make my root element an ImageView instead of a LinearLayout, the prefix is accepted. So it seems to be just a problem using the namespace prefix in a child element.
Also, if I try to add another xmlns:custom="http://schemas.android.com/apk/res-auto" attribute to the ImageView, it complains as well.
If it helps, here is the attrs.xml file I'm using with the above:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<attr name="my_tag1" format="string"/>
<attr name="my_tag2" format="boolean"/>
</resources>
I've seen some stuff online that leads me to believe what I want to do should be possible. For example, in the accepted answer here, Qberticus uses the prefix "whatever" in a child class. Similarly in the post here.
I don't get it. Is using a non-android namespace prefix just not allowed for child elements, or am I doing something wrong?
It can be ignored, I always did it and I didn't encounter any problems.
To make it go away you need to set the following:
add xmlns:tools="http://schemas.android.com/tools" to your root view
add tools:ignore="MissingPrefix" to your text view
On a side note, however, I usually use custom attributes with custom views :-)
I create a custom control as a android library. Everything is OK when i use this control in android project if i declare and use it programmatically, but i can't use this on XML so i follow this tut Declaring a custom android UI element using XML. In the first step, I meet this error
ERROR: In <declare-styleable> myView, unable to find attribute a:gender
ERROR: In <declare-styleable> myView, unable to find attribute a:location
....
And also this on R.java file
Syntax error, insert "}" to complete ClassBody
this is my values\attrs.xml on library
<?xml version="1.0" encoding="UTF-8"?>
<resources>
<declare-styleable name="myView">
<attr name="a:location"/>
<attr name="a:gender"/>
</declare-styleable>
</resources>
Am I missing something?. Waiting for your help. Thanks you!
Answer for my own question. Just remove "a:" before attr name everything will be fine.
<?xml version="1.0" encoding="UTF-8"?>
<resources>
<declare-styleable name="myView">
<attr name="location"/>
<attr name="gender"/>
</declare-styleable>
</resources>
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