I have custom component extends LinearLayout. I need create attribute for putting custom method for this component. And I need call it inside component.
Like as Button.
<Button android:onClick="customMethod"/>
How to fix it?
I have custom component extends LinearLayout. I need create attribute for putting custom method for this component. And I need call it inside component.
Like as Button.
<Button android:onClick="customMethod"/>
How to fix it?
Have you seen this post:
Android - Writing a custom (compound) component
Could be the answer you are looking for.
Looking at the source code of the View
class in the Android java file you can find the following
case R.styleable.View_onClick:
if (context.isRestricted()) {
throw new IllegalStateException("The android:onClick attribute cannot "
+ "be used within a restricted context");
}
final String handlerName = a.getString(attr);
if (handlerName != null) {
setOnClickListener(new OnClickListener() {
private Method mHandler;
public void onClick(View v) {
if (mHandler == null) {
try {
mHandler = getContext().getClass().getMethod(handlerName,
View.class);
} catch (NoSuchMethodException e) {
int id = getId();
String idText = id == NO_ID ? "" : " with id '"
+ getContext().getResources().getResourceEntryName(
id) + "'";
throw new IllegalStateException("Could not find a method " +
handlerName + "(View) in the activity "
+ getContext().getClass() + " for onClick handler"
+ " on view " + View.this.getClass() + idText, e);
}
}
try {
mHandler.invoke(getContext(), View.this);
} catch (IllegalAccessException e) {
throw new IllegalStateException("Could not execute non "
+ "public method of the activity", e);
} catch (InvocationTargetException e) {
throw new IllegalStateException("Could not execute "
+ "method of the activity", e);
}
}
});
}
break;
The getMethod
method should be what you are looking for.