How to set android:onClick in xml of a custom view?
<p>I saw this <a href="https://stackoverflow.com/questions/25905086/multiple-buttons-onclicklistener-android">SO question</a>, and want to implement accepted answer for a custom view.</p>

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

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

<p>it is my custom view:</p>

<pre><code>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;
        }
    }

}
</code></pre>

<p>and it is my xml file:</p>

<pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;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"&gt;

    &lt;LinearLayout
        android:layout_height="wrap_content"
        android:layout_weight="wrap_content"
        android:background="@drawable/bg_ctrl"
        android:orientation="vertical"&gt;

        &lt;android.support.v7.widget.AppCompatButton
            android:id="@+id/btn_down"
            style="@style/CustomButton"
            android:onClick="myMethod"
            android:theme="@style/CustomButton.RED" /&gt;

        &lt;android.support.v7.widget.AppCompatButton
            android:id="@+id/btn_down_double"
            style="@style/CustomButton"
            android:onClick="myMethod"
            android:theme="@style/CustomButton.RED2" /&gt;

    &lt;/LinearLayout&gt;

&lt;/LinearLayout&gt;
</code></pre>

<p>it is the error:</p>

<pre><code>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'
</code></pre>

<p><strong>UPDATE:</strong></p>

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