Is there an easy way to declare the listener in XML (similar to the android:onClick=”onMyButtonClick” in the stock controls)?
This is a great question, and in answering his question, I’ll go ahead and show you how to create other custom attributes you can put in the XML for your custom widgets. The goal will be something like this in your layouts:
<com.kdion.tutorial.MyCustomWidget android:id="@+id/theId" <!-- ...more stuff here... --> custom:myText="Something" custom:fancyColors="true" custom:onAction="myDoSomething" />
We have a custom
MyCustomWidget, and along with the standard
android: attributes, we have a few of our own:
onAction="doSomething". Presumably, these would map to attributes we already have for our custom widget which we could set in code, but we want a shortcut to set them from XML.
Continue reading Custom XML Attributes For Your Custom Android Widgets