How do I declare an Android UI element using XML?
|
The Android Developer Guide has a section called Building Custom Components. Unfortunately, the discussion of XML attributes only covers declaring the control inside the layout file and not actually handling the values inside the class initialisation. The steps are as follows: 1. Declare attributes in
|
|
I would like to add that if your root element requires your custom namespace, you will have to add both the standard android namespace and your own custom one or else you may experience build errors. – Chase Sep 2 '11 at 4:14 |
||
|
This answer is the clearest resource on the Internet on custom XML params that I was able to find. Thank you, Casebash. – Artem Russakovskii Oct 8 '11 at 1:34 |
||
|
|||
|
for some reason , the visual editor refuses to use the written text value for android:text , yet the device uses it just fine . how come ? – android developer Nov 12 '12 at 19:27 |
||
|
@androiddeveloper It seems that Eclipse editor refuses to use the values for all android: attributes. I would like to know if it is a feature or bug – deej Mar 28 at 0:17 |
Great reference. Thanks! An addition to it: If you happen to have a library project included which has declared custom attributes for a custom view, you have to declare your project namespace, not the library one's. Eg: Given that the library has the package "com.example.library.customview" and the working project has the package "com.example.customview", then: Will not work (shows the error " error: No resource identifier found for attribute 'newAttr' in package 'com.example.library.customview'" ):
Will work:
|
|||||||
|
It seems that Google has updated its developer page and added various trainings there. One of them deals with the creation of custom views and can be found here Best, Michael |
|||
|
Addition to most voted answer. obtainStyledAttributes()I want to add some words about obtainStyledAttributes() usage, when we create custom view using android:xxx prdefined attributes. Especially when we use TextAppearance.
What we can see here? What is not mentioned in documentation - order of result TypedArray elements. And other question is: "How we can replace internal constants, and request standard attributes?" We can use android.R.attr.* values. So if we want to use standard TextAppearance attribute in custom view and read its values in constructor, we can modify code from TextView this way:
Where CustomLabel is defined:
Maybe, I'm mistaken some way, but Android documentation on obtainStyledAttributes() is very poor. Extending standard UI componentAt the same time we can just extend standard UI component, using all its declared attributes. This approach is not so good, because TextView for instance declares a lot of properties. And it will be impossible to implement full functionality in overriden onMeasure() and onDraw(). But we can sacrifice theoretical wide reusage of custom component. Say "I know exactly what features I will use", and don't share code with anybody. Then we can implement constructor |
|||||||||
|
Thanks a lot for the first answer. As for me, I had just one problem with it. When inflating my view, i had a bug : java.lang.NoSuchMethodException : MyView(Context, Attributes) I resolved it by creating a new constructor : public MyView(Context context, AttributeSet attrs) {
} Hope this will help ! |
|||
|