1

I saw this SO question, and want to implement accepted answer for a custom view.

Because in the xml of my custom view, it doesn't recognize any method for android:onClick for any buttons! I wonder if it is because of custom view, maybe I should just use onClick method in xml of an activity.

Is it possible to set a method for a button android:onClick attribute in a user-defined custom view (which extends LinearLayout), like below:

it is my custom view:

public class CustomEditText extends LinearLayout{
...
    public void myMethod(View v) {

        switch (v.getId()) {
        case R.id.btn_down:
            // do stuff;
            break;
        case R.id.btn_down_double:
            // do stuff;
            break;
        }
    }

}

and it is my xml file:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:orientation="horizontal">

    <LinearLayout
        android:layout_height="wrap_content"
        android:layout_weight="wrap_content"
        android:background="@drawable/bg_ctrl"
        android:orientation="vertical">

        <android.support.v7.widget.AppCompatButton
            android:id="@+id/btn_down"
            style="@style/CustomButton"
            android:onClick="myMethod"
            android:theme="@style/CustomButton.RED" />

        <android.support.v7.widget.AppCompatButton
            android:id="@+id/btn_down_double"
            style="@style/CustomButton"
            android:onClick="myMethod"
            android:theme="@style/CustomButton.RED2" />

    </LinearLayout>

</LinearLayout>

it is the error:

FATAL EXCEPTION: main
java.lang.IllegalStateException: Could not find method myMethod(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatButton with id 'btn_down'

UPDATE:

parent of this custom view is a fragment and I moved myMethod() to that fragment, but the error still insists :-|

Community
  • 1
  • 1
Parissa Kalaee
  • 606
  • 1
  • 9
  • 17

3 Answers3

2

This has to be in your activity class code:

    public void myMethod(View v) {

        switch (v.getId()) {
          case R.id.btn_down:
            // do stuff;
            break;
          case R.id.btn_down_double:
            // do stuff;
            break;
        }
    }
Mussa
  • 1,463
  • 21
  • 25
  • thank you Mussa for the reply, it is actually a fragment (the host for this custom view), and when I define myMethod() in the fragment, it doesn't make any difference :( in my xml file, this myMethod is not recognized :-| – Parissa Kalaee Feb 21 '16 at 16:33
  • This method has to be in activity, not fragment. – Mussa Feb 21 '16 at 19:24
1

Please define 'myMethod()' in the activty class.

If you want to add it in fragment then you have to define it in onClickListener of the button and if you want to call that method on multiple buttons then you can make a private method and call it from multiple onClickLisetner.

More on onClick xml method and onClickListener:

How exactly does the android:onClick XML attribute differ from setOnClickListener?

Community
  • 1
  • 1
Rohit Gulati
  • 542
  • 3
  • 15
  • thank you Rohit, I got it, however, as I wanted to use that method locally in custom view (.jar file), then the true answer is that in my case it is not the good solution to call the method in android:onClick (in xml file) – Parissa Kalaee Feb 22 '16 at 04:35
0

like the accepted answer and @ROHIT's answer, it is not possible to define myMethod() in fragment/custom view (.jar file) to use in any .xml file...

so to call myMethod() in android:onClick="myMethod", the definition of myMethod should be firstly public and secondly in the Activity!

Although, in my case, it is not the solution, because I want to use myMethod() in CustomEditText.jar to easily access to local variables (especially in the case that I copy that custom view for different usages).

Parissa Kalaee
  • 606
  • 1
  • 9
  • 17